welcome to Tong's Digital Garden
bigguaiwutong@qq.com
by
线程的分类:用户线程(User Thread)、守护线程(Daemon Thread)
public class TestProject {
public static void main(String[] args) {
new WorkerThread().start();
try {
Thread.sleep(7500);
} catch (InterruptedException e) {
// handle here exception
}
System.out.println("Main Thread ending") ;
}
}
public class WorkerThread extends Thread{
public WorkerThread() {
// 设置是否为守护线程
// 守护线程只有在初始化时能进行设置,不然就会直接从父类继承,默认为用户线程
setDaemon(false);
}
public void run() {
int count = 0;
while (true) {
System.out.println("Hello from Worker "+count++);
try {
sleep(5000);
} catch (InterruptedException e) {
// handle exception here
}
}
}
}
可以在设置为用户线程和守护线程时分别执行。