默认情况下,Java 进程需要等待所有线程都运行结束,才会结束。有一种特殊的线程叫做守护线程,只要其它非守 护线程运行结束了,即使守护线程的代码没有执行完,也会强制结束。
log.debug("开始运行...");
Thread t1 = new Thread(() -> {
while(true) {
boolean interrupted = Thread.currentThread().isInterrupted();
if(interrupted) {
log.debug("被打断了, 退出循环");
break;
}
}
}, "daemon");
t1.start();
sleep(1);
log.debug("运行结束...");
/*
运行结果: 主线程运行完, 结束,流程不会结束,t1线程还在运行
08:26:38.123 [main] c.TestDaemon - 开始运行...
08:26:39.215 [main] c.TestDaemon - 运行结束...
*/
非守护线程结束,守护线程会立即运行结束
log.debug("开始运行...");
Thread t1 = new Thread(() -> {
log.debug("开始运行...");
sleep(2);
log.debug("运行结束...");
}, "daemon");
// 设置该线程为守护线程
t1.setDaemon(true);
t1.start();
sleep(1);
log.debug("运行结束...");
/*
运行结果:
08:26:38.123 [main] c.TestDaemon - 开始运行...
08:26:38.213 [daemon] c.TestDaemon - 开始运行...
08:26:39.215 [main] c.TestDaemon - 运行结束...
*/
以操作系统层面:
根据 Thread.State 枚举,分为六种状态:
NEW 线程刚被创建,但是还没有调用 start() 方法
RUNNABLE 当调用了 start() 方法之后,注意,Java API 层面的 RUNNABLE 状态涵盖了 操作系统 层面的 【可运行状态】、【运行状态】和【阻塞状态】(由于 BIO 导致的线程阻塞,在 Java 里无法区分,仍然认为 是可运行)
BLOCKED , WAITING , TIMED_WAITING 都是 Java API 层面对【阻塞状态】的细分
TERMINATED 当线程代码运行结束
线程状态案例
@Slf4j(topic = "c.TestState")
public class TestState {
public static void main(String[] args) throws IOException {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("running...");
}
};
// runnable
Thread t2 = new Thread("t2") {
@Override
public void run() {
while(true) {
}
}
};
t2.start();
Thread t3 = new Thread("t3") {
@Override
public void run() {
log.debug("running...");
}
};
t3.start();
// timed_waiting
Thread t4 = new Thread("t4") {
@Override
public void run() {
synchronized (TestState.class) {
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t4.start();
// waiting
Thread t5 = new Thread("t5") {
@Override
public void run() {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t5.start();
// blocked
Thread t6 = new Thread("t6") {
@Override
public void run() {
synchronized (TestState.class) {
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t6.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("t1 state {}", t1.getState());
log.debug("t2 state {}", t2.getState());
log.debug("t3 state {}", t3.getState());
log.debug("t4 state {}", t4.getState());
log.debug("t5 state {}", t5.getState());
log.debug("t6 state {}", t6.getState());
System.in.read();
}
}
/*
运行结果:
22:00:37.048 c.TestState [t3] - running...
22:00:37.554 c.TestState [main] - t1 state NEW
22:00:37.556 c.TestState [main] - t2 state RUNNABLE
22:00:37.556 c.TestState [main] - t3 state TERMINATED
22:00:37.556 c.TestState [main] - t4 state TIMED_WAITING
22:00:37.556 c.TestState [main] - t5 state WAITING
22:00:37.556 c.TestState [main] - t6 state BLOCKED
*/
以泡壶茶喝为例,统计最少花费时间.
洗水壶1分钟,烧开水15分钟,洗茶壶1分钟,洗茶杯2分钟,拿茶叶1分钟,泡茶.
public class Test16 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
log.debug("洗水壶");
sleep(1);
log.debug("烧开水");
sleep(5);
},"老王");
Thread t2 = new Thread(() -> {
log.debug("洗茶壶");
sleep(1);
log.debug("洗茶杯");
sleep(2);
log.debug("拿茶叶");
sleep(1);
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("泡茶");
},"小王");
t1.start();
t2.start();
}
}
/*
运行结果:
22:02:55.910 c.Test16 [小王] - 洗茶壶
22:02:55.910 c.Test16 [老王] - 洗水壶
22:02:56.935 c.Test16 [小王] - 洗茶杯
22:02:56.935 c.Test16 [老王] - 烧开水
22:02:58.936 c.Test16 [小王] - 拿茶叶
22:03:01.935 c.Test16 [小王] - 泡茶
*/