3:50 pm - Tuesday February 19, 9799

Thread Example program for setDaemon(boolean on)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
// Thread Example Program for setDaemon(boolean on)
/**
@author: Candidjava.com
@description: setting a thread as daemon thread, so as it will run in the background,
and when the main method ends the program terminates without caring about the daemon thread.
*/
public class Daemon extends Thread
{
public void run()
{
for(int i=0;i<=10;i++)
{
System.out.println(i);

try{
sleep(1000);
}
catch(Exception e){}
}
}

public static void main(String[] args)
{
Daemon d=new Daemon(); // creating class object 'd'
Thread t=new Thread(d); // creating thread object 't'
t.setDaemon(true); //setting the thread 't' as a daemon thread
t.start(); // starting the run method

for(int j=11;j<=13;j++)
{
System.out.println(j);

try{
sleep(1000);
}
catch(Exception e){}
}
}
}




output:
11
0
12
1
13
2
3

Filed in: Z-A All Java Codes

Comments are closed.