Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads execution is disorder

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Anton Shaikin
Ranch Hand
Posts: 63
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anton Shaikin
Ranch Hand
Posts: 63
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3032
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Shaikin
Ranch Hand
Posts: 63
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic