-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathDaemonThreadDemo.java
More file actions
45 lines (37 loc) · 1.07 KB
/
DaemonThreadDemo.java
File metadata and controls
45 lines (37 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 公司:阿里游戏
* 创建时间:2018年11月2日下午5:56:43
*/
package cn.aofeng.demo.thread;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 守护线程DEMO。
*
* @author <a href="mailto:nieyong.ny@alibaba-inc.com">聂勇</a>
*/
public class DaemonThreadDemo extends Thread {
private static Logger logger = LoggerFactory.getLogger(DaemonThreadDemo.class);
@Override
public void run() {
while (true) {
System.out.println("守护线程运行, 时间:" + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.error("守护线程运行出错", e);
}
}
}
public static void main(String[] args) {
DaemonThreadDemo thread = new DaemonThreadDemo();
thread.setDaemon(true);
thread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
logger.error("主线程运行出错", e);
}
}
}