Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Threads and Synchronization and the fly likes print m1 m2 m3 in 3 different threads in sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "print m1 m2 m3 in 3 different threads in sequence" Watch "print m1 m2 m3 in 3 different threads in sequence" New topic
Author

print m1 m2 m3 in 3 different threads in sequence

Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35


Hi,
THe output Iam getting now is

Thread Name is = Thread M1 m1
Thread Name is =Thread M1 m2
Thread Name is =Thread M1 m3
Thread Name is = Thread M2 m1
Thread Name is =Thread M2 m2
Thread Name is =Thread M2 m3
Thread Name is = Thread M3 m1
Thread Name is =Thread M3 m2
Thread Name is =Thread M3 m3


However, I want thread2 to print m1 after thread 1 prints m1, and thread 3 to print m1 after thread 2.
I mean, the desired output is

m1 (code run by thread 1)

m1 (code run by thread 2)

m1 (code run by thread 3)

m2 (code run by thread 1)

m2 (code run by thread 2)

m2 (code run by thread 3)

m3 (code run by thread 1)

m3 (code run by thread 2)

m3 (code run by thread 3)

Thanks
Faisal
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
We cannot use join() as well since after thread 1, 2 and 3 print m1 in this order, they again need to print m2, m3.

Please advise.

Thanks
Faisal
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40


If you need an exact sequence, then either consider doing it single threaded, or add enough share state so that a thread will only run when it is its turn only.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print m1 m2 m3 in 3 different threads in sequence
 
Similar Threads
wait() releases all locks?
Odd-Even Number print program
Question about wait() and notify()
About Synchronization concept of static and non static method using Class level lock
Threads