• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can we make five threads run in a sequence?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friends,

Can we make five threads run in a sequence?
thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic