aspose file tools*
The moose likes Threads and Synchronization and the fly likes sequential execution of threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "sequential execution of threads" Watch "sequential execution of threads" New topic
Author

sequential execution of threads

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
By "execute", do you mean till the thread finishes its job and dies?

If that's the case, use the join() method:

join()
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Originally posted by Tarek Khojah:
By "execute", do you mean till the thread finishes its job and dies?

If that's the case, use the join() method:

join()

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
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm wondering at this point why do we have threads at all? Why isn't T1 just doing the steps in sequence?

If you want to keep threads, you could also use queues between them. I played with generic piping messages from one task stage to the next with blocking queues in between:

T1 -> Q1 -> T2 -> Q2 -> T3

I had messages arriving in T1 from a MIDI controller at unpredictable intervals and rates. Each message is guaranteed to be handled in sequence by T1, T2, T3.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Hi,
Still i think that we have not made the answer clear as to how we can ensure sequential execution of threads.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
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 ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[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
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
For the sake of being old fan of JR, this question can not go unanswered :-)

Check the following link :
http://www.avajava.com/tutorials/lessons/how-do-i-use-threads-join-method.html

copy & pasted from above link:


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1029
    
  15
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.
manjul sharma
Greenhorn

Joined: Sep 04, 2014
Posts: 1
there is 2 ways you can achieve this
1) ExecutorService executor = Executors.newFixedThreadPool(1);
2) t1.start();
Thread.sleep(100);

for your reference ::
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(t1);
executor.submit(t2);
executor.submit(t3);
executor.shutdown();

t1.start();
Thread.sleep(100);
t2.start();
Thread.sleep(100);
t3.start();
 
 
subject: sequential execution of threads