I got different output of this Multi threading code in eclipse and Netbeans ide, i don't know how's it come, and what is the logic behind this.
And Every Time when i execute this code, it will show different output , Plz Help Me.
public class MyThread2 extends Thread
{
public void run()
{
`System.out.println("r1");` ``
try ``
{
Thread.sleep(500);
}
catch(Exception e)
{
}
System.out.println("r2");
}
public static void main(String args[])
{
MyThread2 t1=new MyThread2();
MyThread2 t2=new MyThread2();
t1.start();
t2.start();
System.out.println(t1.isAlive());
System.out.println(t2.isAlive());
}
}
Output in Eclipse:
r1
r1
true
true
r2
r2
and Output in Netbeans :
r1
true
true
r1
r2
r2
Thread are sceduled by JVM. JVM selects to run a Runnable thread with the highest priority. Whenever a new Java thread is created it has the same priority as the thread which created it. SO,in your case both the threads have same priority! Hence,JVM using its own algorithm(Round Robin Scheduling) to pick up the thread and select and execute them in that order. It doesn't have a fixed order and it is totally unpredictable to rate the execution!
It'll give you different output for different sample runs on the same IDE,i.e., either Netbeans or Eclipse. You can try running both! It may be out of order as previous run as these are scheduled by JVM.
Threads are scheduled by JVM and in no particular order. That's the reason you see different output for the same code involving threads.
JVM uses internal OS underlying threads management to manage threads and in almost all of the available OS, the algorithm for threads scheduling is round-robin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With