forked from Pamgo/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
守护线程,但一个java运用只有守护线程的时候,java虚拟机就会自然退出
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |