• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Different thread running schedule

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the thread is concerned there is no guarantee that some set of threads will run in a particular manner.
Lets say I have three different threads for an stock application.

Thread one=new thread(nr); \\This thread will download the stock price
Thread two =new Thread(nr); \\This thread will update the stock into database table
Thread three =new Thread(nr); \\This thread will show the stock price from the database to the stock application's local users.


Now when I run this application in my JVM it can behave in sequence i.e first thread one will run and then Thread two and finally Thread three.

But this behavior is not guaranteed.So if any other JVM runs the same piece of code in that case it may not run in the same sequence.But that is not required.I want whateverr JVM we run this code it should run in the same sequence as it ran in my JVM (i.e First Thread one will run and then Thread two and finally Thread three.)

My question is how can I make sure of the running sequences of these threads with respect to any JVM. And the sequence is very important as the threads are depending on each other.(Will join() method will do this)

Apart from JOIN() method how can we guarantee the same.

Thanks.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "run in the same sequence as it ran in my JVM?"

Do you mean one runs to completion. Then two starts and runs ....

In such a case why bother with threads.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward..

I mean the way it ran on my JVM.

First it ran the Thread one then Thread two and finally Thread three.

Thread one will run to completion first then Thread two and finally Thread three.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have tasks which must be run in sequence, one after another, then why are you using Threads? Threads are used to run tasks in parallel. Using threads to run tasks in sequence is a waste of effort. Why not just run the tasks in one thread?
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,

I understand what you are telling but my question is if I do the tasks in such approach(what ever is posted in my last post) it will differ in different JVM, so in that case how can we guarantee the sequence of the running THREADS.

Thanks!
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aneek Banerjee wrote:
I understand what you are telling but my question is if I do the tasks in such approach(what ever is posted in my last post) it will differ in different JVM, so in that case how can we guarantee the sequence of the running THREADS.


If the threads needs to behave is a particular sequence, then they need to communicate with each other. The easiest way is with shared variables -- which of course, needs to be protected with synchronization. Other options are with notification objects -- wait and notify. And many other options in the java.util.concurrent package.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic