public class JavaDaemonThread {
public static void main(String[] args) throws InterruptedException {
// Thread dt = new Thread(new DaemonThread(), "dt");
// Thread dt = new Thread(new DaemonThread()); //简写
DaemonThread t1 = new DaemonThread();
Thread dt = new Thread(t1);
dt.setDaemon(true);//译者注:此次将User线程变为Daemon线程 【守护线程】
dt.start();
//continue program
Thread.sleep(5000);
System.out.println("Finishing program");
}
}
class DaemonThread implements Runnable{//译者注:此类虽类名是为Daemon线程,其实为User线程
@Override
public void run() {
while(true){
processSomething();
}
}
private void processSomething() {
try {
System.out.println("Processing daemon thread");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
// Thread dt = new Thread(new DaemonThread(), "dt");
// Thread dt = new Thread(new DaemonThread()); //简写
DaemonThread t1 = new DaemonThread();
Thread dt = new Thread(t1);
dt.setDaemon(true);//译者注:此次将User线程变为Daemon线程 【守护线程】
dt.start();
//continue program
Thread.sleep(5000);
System.out.println("Finishing program");
}
}
class DaemonThread implements Runnable{//译者注:此类虽类名是为Daemon线程,其实为User线程
@Override
public void run() {
while(true){
processSomething();
}
}
private void processSomething() {
try {
System.out.println("Processing daemon thread");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}