Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Khurram Fakhar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guyz , consider a scenario where there is one source and multiple threads (lets say 4 threads) are accessing it. Why it happen that even we write thread1.start() first and then start() of second thread and then third one and then start of fourth thread, but we can never give surity that thread1 will access the source first.
I hope scenario is clear.
waiting for reply
regards
khurram fakhar
 
sushant
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Khurram Fakhar,
The reason that u cannot expect which thread will execute and when is that, no matter in what order u start the threads, it is upto the jvm to decide which thread gets to run.
So the bottomline is that u cannot predict which thread gets to execute from a pool of threads.
hope that answers ur question.
Sushant.
 
Khurram Fakhar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx sushant , but i know this thing already,
problem is how JVM decides i mean upon which algorithm that which thread should execute first . SOME Random strategy or what.
that thing is confusing me nothing else
Regards
Khurram Fakhar
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hie,
Actually it is not entirely, if at all, the responsibility of the JVM to decide which threads execute first, and in what order.It is up to the actual native thread implementation.Java's thread scheduling policy ensures that all threads get at least the same chance to execute,regardless of priorities,but recall that Java threads are only an abstraction of real native threads of execution, hence it is up to the implementation of these that thread behavior can be guaranteed.You can however, implement your own thread scheduler and make thread behaviour deterministic at application level.
Regards,
Herbert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic