• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we make five threads run in a sequence?

 
prabhu deva
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,

Can we make five threads run in a sequence?
thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Sure -- you could do this:



The join() method returns only when the Thread terminates.

But why would you want to do this?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With JDK 5 you could also queue up tasks for a thread pool with only one thread. That would let you queue any number of tasks to run in parallel with your main thread, and know that only one of them runs at a time. I can almost imagine that being a real requirement.

I guess you could also start all five and have them synchronize on a shared object for almost the same effect. Sequencing their access to some shared resource is a bit easier to imagine as a requirement.
 
Saurabh Dixit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way of achieving sequential running of five threads is assigning priority to each thread. the high priority thread will run first.
Also we may provide delay between thread executions to make them run sequentially
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saurabh Dixit:
One way of achieving sequential running of five threads is assigning priority to each thread. the high priority thread will run first.


Sorry, but there's no guarantee that you have five distinct priority levels, or especially that high-priority threads will completely exclude lower-priority threads from running.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!