I have four threads t1,t2,t3,t4 acting on same object of the class.I have some restriction,t2 should execute only after t1 has executed and t3 should execute only after t2 has executed,and same is the case with t4.I mean sequence should compulsarily be t1 then t2 then t3 then t4.How can we make sure that these four threads will execute in sequence only.
You need to synchronize; look at the java.util.concurrent and java.util.concurrent.locks packages.
Basically, you need to have the threads (except t1) block until its predecessor releases that lock.
To keep the number of objects low, you could use a Semaphore, and initialize it with 3 permits. Acquire all those permits immediately.
Now t1 doesn't have to wait, so it does not need to acquire any permits. At the end it should release 1 single permit.
t2 should try to acquire one single permit; it will block until one is available, which will not happen until after t1 releases 1. At the end it should release 2 permits.
t3 should try to acquire 2 permits; it will block until 2 are available, which will not happen until after t2 releases 2. It will not take the single released permit from t1. At the end it should release 3 permits.
t4 should try to acquire all 3 permits; it will block until 3 are availabe, which will not happen until after t3 releases 3. It will not take the 1 or 2 released permits from t1 or t2. No need to release anything at the end.
Damn, you're right, and your solution is so much easier than mine.
At the start of each thread (except t1), make it call join() on it's predecessor. That's it.
Raj Kumar Bindal
Joined: Apr 15, 2006
Thanks for the reply. But we can do t2.join only after t2.start(),and there is a probability that t2 has already entered the concerned method.So,this case can fail.So,how we will make sure that t1 will finish its execution first. If possible,please continue this post with some code,because it becomes more clear with that.
If you want sequential execution, why on earth are you using threads at all? It sounds like a profoundly silly question. If you don't want things to execute concurrently, don't put them in separate threads.
"I'm not back." - Bill Harding, Twister
Joined: Jan 29, 2003
I think you have several answers.
Something like that will do what you want. As Jim and I both suggested, it's a completely pointless exercise, however. If you want to run things in sequence, just write them in sequence. If you want to run them in sequence on another thread, put the sequence in a single Runnable.
[ December 09, 2007: Message edited by: Stan James ]
As Stan and Jim stated, I don't see any need for using threads here.What you need is a purely sequential flow of control.You can just put together the contents of the run() method of all the threads ,in the order you want,in a single method in some client program and call it from the main() method.
If you still want threads( even if they are absolutely unnecessary here),try the following.
Note,however, that the above piece of code doesn't really trigger the creation of any OS thread as such. The entire chunk executes in one single thread( the one that executes the above code),even though we have created four Thread Objects, the reason being t1,t2..etc are all plain java objects and there's no multithreading happening here,as we don't invoke start() on any of the threads. [ December 10, 2007: Message edited by: san ban ]
[san ban:] As Stan and Jim stated, I don't see any need for using threads here.What you need is a purely sequential flow of control.You can just put together the contents of the run() method of all the threads ,in the order you want,in a single method in some client program and call it from the main() method.
Correct, then if other work should be done in the objects after some sequential work, then run(); as you have it here could be renamed init(); and start(); could be called on each runnable after any sequential matters finished.
[Raj Kumar Bindal:] Still i think that we have not made the answer clear as to how we can ensure sequential execution of threads.
Stan James or san ban's code example will insure purely sequential flow of control. Let us know if there are further uncertianties.
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
R K Singh wrote:For the sake of being old fan of JR, ......................
I don't know what point you're trying to make, but those comments that you've put there are wrong.
The instruction will make the current thread, i.e the main thread, wait for the thread1 to finish. In other words, the next instruction
can execute ( main can resume ) only after thread1 has ended. Similarly causes the main thread to join thread2, i.e the main thread can resume only after thread2 has finished.