• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Printing from threads in a particular sequence

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:-
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic