wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes I want to execution of a Threads in sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "I want to execution of a Threads in sequence" Watch "I want to execution of a Threads in sequence" New topic
Author

I want to execution of a Threads in sequence

Bk Jacky
Ranch Hand

Joined: Jun 11, 2005
Posts: 74
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...


SCJP1.4<br />SCWCD1.5<br />"Nothing is impossible"
Surya Kant
Ranch Hand

Joined: Mar 29, 2005
Posts: 104
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

Joined: Mar 29, 2005
Posts: 104
Why dont you try wait and notify methods?
It is not possible to achieve the same with priority settings.
Bk Jacky
Ranch Hand

Joined: Jun 11, 2005
Posts: 74
Can you please explain me with some sample code.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18522
    
  40

http://java.sun.com/docs/books/tutorial/essential/concurrency/


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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


Steve
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
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!


regards,
Arka
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73

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.

Regards,
Alan Mehio
London, UK
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
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

Joined: Jun 11, 2005
Posts: 74
Thanks all for you kind help specially Alan and Luke.
 
Consider Paul's rocket mass heater.
 
subject: I want to execution of a Threads in sequence
 
Similar Threads
JQ+ thread question
URLyBird 1.3.1: Locking Problem
A question of lock
B&S: Locking
synchronized and Thread