This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Ensuring two Runnables run in order

 
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.
 
Oh the stink of it! Smell my tiny ad!
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!