• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Different thread running schedule

 
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • 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.
 
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 Tomcat Server Java
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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 Tomcat Server Java
  • 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!
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic