GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Threads execution is disorder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads execution is disorder" Watch "Threads execution is disorder" New topic
Author

Threads execution is disorder

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
Hi , iam new to Thread concepts and ran my first thread program.
following is the output:-



ThreadExample.java:-


A.java:-


B.java:-


C.java:-




2nd Output:-



3rd output:-


Everytime the program executes, different output comes in different order.
My question is does this thread executes always in such unpredictable order ?
Is there any way to control the order of execution according to us ?


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

Hi, Vinod
My question is does this thread executes always in such unpredictable order ?

Yes, it does. You start three threads but your JVM decides only at run-time which thread gets CPU time. However you can use setPriority() method of Thread class, and in this case, Thread with higher priority gets higher chance to be run, but again it doesn't guarantee anything. Other methods that could improve "stability" of output are sleep() and yield(). But againg, even if you call, yield(), it's up to thread scheduler to decide which thread to run. You should probably read about synchronization, but that is somewhat complex topic, and I'm myself having hard time with it
Good luck!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Anton Shaykin wrote:Hi, Vinod
My question is does this thread executes always in such unpredictable order ?

Yes, it does. You start three threads but your JVM decides only at run-time which thread gets CPU time. However you can use setPriority() method of Thread class, and in this case, Thread with higher priority gets higher chance to be run, but again it doesn't guarantee anything. Other methods that could improve "stability" of output are sleep() and yield(). But againg, even if you call, yield(), it's up to thread scheduler to decide which thread to run. You should probably read about synchronization, but that is somewhat complex topic, and I'm myself having hard time with it
Good luck!



yes......shchronization is a very gud way to control the execution of the thread.......with the help of sychronication you can use.........join() method.....so that till the 1 thread executes completely......next thread will not execution.............
synchronized means it will occupy some locking condition between the thread execution


SCJP6.0,My blog Ranchers from Delhi
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

yes......shchronization is a very gud way to control the execution of the thread.......

Synchronization and join() method are in no way connected. join() could be used out of synchronized context.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
Anton Shaykin wrote:
yes......shchronization is a very gud way to control the execution of the thread.......

Synchronization and join() method are in no way connected. join() could be used out of synchronized context.

Actually, join() uses synchronization internally. So they certainly are connected. However, when using join(), there is no need to explicitly use synchronization. That happens automatically behind the scenes, thanks to the implementation of join().
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

Actually, join() uses synchronization internally

Thanks for the information. Actually I wasn't digging too deep, and assuming that in this particular case no knowledge of synchronization is required, I don't think it's a good idea to scare Java greenhorns with such advanced concepts
 
GeeCON Prague 2014
 
subject: Threads execution is disorder