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
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.
Joined: May 29, 2000
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
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