File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Different thread running schedule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Different thread running schedule" Watch "Different thread running schedule" New topic
Author

Different thread running schedule

Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

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

Joined: Sep 19, 2005
Posts: 291

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.


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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?


Steve
Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Different thread running schedule
 
Similar Threads
Thread,Join problem
Will a started thread run to completion in this example?
Threads
Is Kathy Sierra right when she said "Each thread will start, and each thread will run to completion"