setDaemon(true)后就是后台线程(守护线程 ),反之就是前台线程(用户线程)
2011-09-27 09:38
477 查看
1,setDaemon(true)后就是后台线程(守护线程 ),反之就是前台线程(用户线程)
2,后台线程 和 前台线程的区别:在java程序中如果所以的前台线程都已经退出,所有的后台线程自动退出。
TestThread为后台线程:
Java代码
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
} 运行当前台线程main退出后自动退出。
TestThread和main一样也是前台线程:
Java代码
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
// t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
// t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
}
TestThread 就不会退出。
也就是说,所有的前台线程结束后,所有的后台线程会自动退出。
2,后台线程 和 前台线程的区别:在java程序中如果所以的前台线程都已经退出,所有的后台线程自动退出。
TestThread为后台线程:
Java代码
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
} 运行当前台线程main退出后自动退出。
TestThread和main一样也是前台线程:
Java代码
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
// t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
// t.setDaemon(true);
t.start();
// while(true) {
// System.out.println("main(): "+Thread.currentThread().getName() + " is running");
// }
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
}
TestThread 就不会退出。
也就是说,所有的前台线程结束后,所有的后台线程会自动退出。
相关文章推荐
- 利用setDaemon(true)设置守护线程
- Daemon线程的意义--监控、回收等后台服务,优先级比较低,无用户线程时,守护线程自尽,JVM exist
- Java-用户(前台)线程和守护(后台)线程
- 守护线程setDaemon(true)
- Java【多线程知识总结(2)】调用setDaemon(true)变成后台线程
- Java【多线程知识总结(2)】调用setDaemon(true)变成后台线程
- JAVA——守护线程或用户线程(setDaemon)
- Java多线程 小知识 守护线程(后台线程)setDaemon、join方法、优先级、yield方法
- Java【多线程知识总结(2)】调用setDaemon(true)变成后台线程
- ASPNET中实现在线用户检测(使用后台守护线程)
- JavaSE8基础 多线程setDaemon 守护线程
- User Thread(用户线程)和Daemon Thread(守护线程)
- Thread.setDaemon() --守护线程
- 二、ASPNET中实现在线用户检测(使用后台守护线程)
- java开发之线程(守护线程daemon和用户线程User Thread)
- java停止线程 interrupt 和守护线程setDaemon
- java的调度、优先级setPriority、yield、jion和守护线程setDaemon
- Java基础多线程之后台守护线程,setDaemon(true)
- 多线程Thread与后台线程setDaemon,使用Runnable接口创建多线程
- 32-多线程(wait和sleep的区别)1 2 33-多线程(停止线程方式-定义标记) 35-多线程(守护线程-setDaemon).1 2