• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ensuring two Runnables run in order  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the code:


I want to ensure that the Runnable started in startThat() cannot run before the Runnable in startThis() completes.

Is using a Semaphore the proper decision ?
My idea is that I will acquire a Semaphore in startThis() and only release it when I'm done.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the code stands it would be simplest to apply a join on your new thread but I guess you need main thread execution to continue, in which case why not just run the 'run' of thread 2 'start that' directly from the end of 'startThis' created thread 'run' i.e. good old non threaded , you'ed have two threads your main one and startThis / StartThat hybrid ... I guess what I'm saying is it its hard to see any point in having two start threads if one can only ever start after the other has completed, I'm reading between the lines and assuming you have something far more complex in mind ;-)

Though if you must have three threads then just have them share a global lock that the first thread will notify on at completion and the second thread waits on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!