Skip to content

Commit

Permalink
守护线程,但一个java运用只有守护线程的时候,java虚拟机就会自然退出
Browse files Browse the repository at this point in the history
  • Loading branch information
Pamgo committed Mar 4, 2018
1 parent bfa84f8 commit b2ec220
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/com/example/concurrency/st/DaemonDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.concurrency.st;

/**
* 守护线程,但一个java运用只有守护线程的时候,java虚拟机就会自然退出
* @author OKali
*
*/
public class DaemonDemo {

public static class DaemonT extends Thread {

@Override
public void run() {
while (true) {
System.out.println("I am alive");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) throws InterruptedException {
Thread t = new DaemonT();
t.setDaemon(true); // 如果不设置为守护线程,则会一直打印输出
t.start();
Thread.sleep(2000);
}
}

0 comments on commit b2ec220

Please sign in to comment.