aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Main Thread is Alive ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Main Thread is Alive ?" Watch "Main Thread is Alive ?" New topic
Author

Main Thread is Alive ?

Vadiraj Deshpande
Ranch Hand

Joined: Aug 16, 2000
Posts: 78
Hi All,
Here is a program which says main thread dies before the child thread. Is that happening in reality ?
check this out --
regards
vadiraj
----------------------------------------------
class NewThread implements Runnable
{
String name;
Thread t,mainThread;
NewThread(Thread mainThread,String tname)
{
name=tname;
this.mainThread=mainThread;
t = new Thread(this,name);
System.out.println("New Thread : " + t);
t.start();
}
public void run()
{
System.out.println("Main Thread is Alive in the start of run meth of "+name+" : " + mainThread.isAlive());
try
{
for(int i=3;i>0;i--)
{
System.out.println(name + ": " +i);
System.out.println("Main Thread is Alive in the run meth of "+name+" Thread : " + mainThread.isAlive());
Thread.sleep(1000);
}
} catch(InterruptedException e)
{}
System.out.println(name + " Exiting");
System.out.println("Main Thread is Alive in the end of run meth of "+name+" Thread : " + mainThread.isAlive());
}
}
public class MainThreadisAlive
{


public static void main(String a[])
{
Thread t=Thread.currentThread();
System.out.println("Main Thread : " + t);
NewThread child1 = new NewThread(t,"Thread1");
NewThread child2 = new NewThread(t,"Thread2");
int i=0;
try
{
for(i=0;i<5;i++)
{
t.sleep(100); // Sleep time is less than the sleep time of the child threads.
System.out.println("Main Thread: " + i);
}
} catch(InterruptedException e)
{
}
System.out.println("Exiting main Thread");
System.out.println("Main Thread is Alive in the end of main: " + t.isAlive());
}
}
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main Thread is Alive ?
 
Similar Threads
Thread Problem
Evidence 4 Main Thread dead after main method exits
Threads
Thread qstn
Threads