i have a problem:-
I have four thread each printing messages on to the printer and i want to run them in a particular sequence ie
my four threads are :- hello , world ,welcome and Mythread and i want them to be executed in the same order .
I tried using wait notifyAll in my program , but all i can control is printing the hello and am unable to print world , welcome and Mythread in the respective sequence . can anyone explain whether it is possible to do what i desire to .
If you want them to run sequentially then just run them in a single thread. The whole idea of threading is that you can run things in parallel. Another thing that you can do is joining the threads but that basically does the same as running them in a single thread.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Agreed. Making multiple threads behave like a single thread is a lot of work to accomplish little. However, if this is an exercise...
You have a few options. You can use multiple condition variables, where each thread will trigger the next one on completion. Or you can add a state variable to the process; each thread need to check the state before it proceeds; sets the state upon completion for the next thread; and trigger the condition for the next one.