• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 23951
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
 
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic