• 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

Running Threads in Succession

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the best way to run a series of threads in succession?
For example, I want to start the first thread and then when that thread is completely finished, I want to start the second thread, etc.
So, is there a method which will tell me when the first thread has completed its task?
I was thinking I would just try starting the next thread at the end of the run() method. Maybe with some type of flag - will that work?
Any better suggestions?
Thanks!
Drew
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
you might want to check the thread api. there is the method join().api says: "Waits for this thread to die." . check also the method isAlive() :"Tests if this thread is alive. A thread is alive if it has been started and has not yet died."
never used this but i think you can work with this.
karl
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This probably won't be as interesting a solution, but you could just call each run method in order without invoking the scheduler -- that'll achieve the same end.
All Thread.start() does is onvoke the scheduler and allow you code run to be managed by the VM. So if you just call run() directly in your threaded code, it'll run to completion before the next run() method starts.
Is there a particular reason you want to do this? Maybe some background will help us understand your objectives better.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,
Thanks for the reply. It's very interesting what you are saying.
I think it would be a little bit complicated (and long winded)to try and explain exactly what I am doing, but essentially I am invoking another program (which has its own thread) by calling its constructor and then extracting some information from the other program by starting a seperate thread. I then need to repeat the process several times (with different params) in sequential order to achieve the desired results.
I guess you could say that I am doing my own scheduling?
Anyway, is calling the run() method directly different than calling another method that I make up myself, say foo() ?
I actually tried using my own method (not called run), but found that it wouldn't work because it wouldn't let the other program that I am invoking finish its thread. Make sense?
Thanks!
Drew
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic