fool fool - 5 months ago 11
Java Question

Multi-Threading in JAVA concept

class MyThread extends Thread
{
public void run()
{
try
{
for(int i=0 ; i<10 ; i++)
{
System.out.println("I am Lazy Thread.");
Thread.sleep(2000);
}
}
catch (InterruptedException e)
{
System.out.println("I got interrrupted.");
}
}
}
class Test
{
public static void main(String[] args)
{
MyThread t = new MyThread();
t.start(); //after this line
t.interrupt();
System.out.println("end of main.");
}

}


After
t.start();
and
t.interrupt();
why it is printing end of main first then the child thread statement .

According to multi threading concept it may be child class thread statement also but it is always executing the main Thread statement first .

What is the concept and working procedure behind this .Because its always executing end of main first than others.

Answer

After t.start(); there is two Thread one is main and another is child thread.

both thread are independent to each other ... : main thread initialization already done before the child thread creation and for JVM (not CPU) its is easy to handle and execute the main thread first than the child thread. If JVM goes for child thread execution than definatly it will takes more time to complete . There are some algorithm which works inside JVM which threads it wants to choose first and it is always vary form JVM to JVM.

Comments