File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic


Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
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
khurram fakhar

Joined: Apr 21, 2000
Posts: 26
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.
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
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
Khurram Fakhar
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
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.
I agree. Here's the link:
subject: Threads
It's not a secret anymore!