• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I want to execution of a Threads in sequence

 
Bk Jacky
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I want to execute Threads in sequence.

Suppose I have Three Threads A, B and C

A(Has lock of object) is performing some job and B&C are waiting on that.

when A will release lock B should get the Lock not C

After that C should get the lock

I was thinking about Priority Setting of each thread.But i am not sure it will work or not .

Need your suggestions...
 
Surya Kant
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you try making use of wait() & notify() methods?
It is very easy to do.Priority setting will not work.
 
Surya Kant
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you try wait and notify methods?
It is not possible to achieve the same with priority settings.
 
Bk Jacky
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain me with some sample code.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple wait() notify() would have to be modified with some signals to make sure the proper order is conserved. Here is an example:


Other options would be to have C lock on a different object then A. B initially locks on the same one A is using and wait()s. A notify()s B, B does its work, then synchronizes on the same Object as C and notify()s it. This can be a bit tricky to implement though, you have to be sure all your data you are protecting are properly protected with the double locks, and that you don't get into a deadlock situation.

However, if your target app is at Java 1.5 or later, you should probably take advantage of the java.util.concurrent.Lock/Condition classes. Look up the tutorial for Concurrency in Java and you will see example usage.

/*** Edit ***/
The notify()s should be notifyAll()s, changed in above code
 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way to determine if B will run after A has send notify().Remember notify() invokes a thread from the wait set and then that thread competes for acquisition of lock on an object.Only the Operating system determines which thread will take the lock.Also note that if there are 2 threads X and Y where X has maximum priority and Y has minimum priority and both are in ready to run state...in such a scenario it only increases the chances of X in running state over Y but does not guarantee execution of X before Y.

Good Luck!
 
Alan Mehio
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I was thinking about Priority Setting of each thread.But i am not sure it will work or not .



It is generally wise to resist the temptation to tweak thread priorities. As soon as you start modifiying priorities, the behavior of your application becomes platform-specific and you introduce the risk of starvation.
 
Alan Mehio
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just made some correction based on Steve Luke code. Basically the notifiier will triggers the first thread to run and will wait until all get executed


I hope this correction reinforce the order for thread execution.
 
Bk Jacky
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for you kind help specially Alan and Luke.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic