Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Main Thread is Alive ?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
------------------
reply
    Bookmark Topic Watch Topic
  • New Topic