Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing from threads in a particular sequence  RSS feed

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,


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 .

My code is as follows:-
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!