• 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

Threads execution is disorder

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 65
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 65
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 65
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You firghten me terribly. I would like to go home now. Here, take this 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