File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sequential execution of threads

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Chan Ag
Rancher
Posts: 1089
14
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic