public static void main(String[] args) {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug(Thread.currentThread().getName());
FileReader.read(Constants.MP4_FULL_PATH);
}
};
t1.run();
log.debug("do other things ...");
}
输出:
19:39:14 [main] c.TestStart - main
19:39:14 [main] c.FileReader - read [1.mp4] start ...
19:39:18 [main] c.FileReader - read [1.mp4] end ... cost: 4227 ms
19:39:18 [main] c.TestStart - do other things ...
可以看到程序仍在 main 线程运行, FileReader.read() 方法调用还是同步的
19:41:30 [main] c.TestStart - do other things ...
19:41:30 [t1] c.TestStart - t1
19:41:30 [t1] c.FileReader - read [1.mp4] start ...
19:41:35 [t1] c.FileReader - read [1.mp4] end ... cost: 4542 ms
可以看到程序在 t1 线程运行, FileReader.read() 方法调用是异步的
yield
其中的清除打断标记指的是无论打断标记是true还是false,interrupted都将它变为false。
在一个线程 T1 中如何“优雅”终止线程 T2?这里的【优雅】指的是给 T2 一个料理后事的机会。
错误思路
使用线程对象的 stop() 方法停止线程
使用 System.exit(int) 方法停止线程
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class test1 {
public static void main(String[] args) throws InterruptedException {
TwoPhaseTermination tpt = new TwoPhaseTermination();
tpt.start();
Thread.sleep(3500);
tpt.stop();
}
}
@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination{
private Thread monitorThread;
public void start(){
monitorThread = new Thread(()->{
while(true){
Thread current = Thread.currentThread();
if(current.isInterrupted()){
log.debug("料理后事");
break;
}
try{
Thread.sleep(1000);
log.debug("执行监控记录");
} catch (InterruptedException e) {
e.printStackTrace();
current.interrupt();
}
}
},"monitor");
monitorThread.start();
}
public void stop(){
monitorThread.interrupt();
}
}
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class test1 {
public static void main(String[] args) throws InterruptedException {
TwoPhaseTermination tpt = new TwoPhaseTermination();
tpt.start();
Thread.sleep(3500);
tpt.stop();
}
}
@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination{
private Thread monitorThread;
public void start(){
monitorThread = new Thread(()->{
while(true){
Thread current = Thread.currentThread();
if(current.interrupted()){
log.debug("料理后事");
break;
}
try{
Thread.sleep(1000);
log.debug("执行监控记录");
} catch (InterruptedException e) {
e.printStackTrace();
current.interrupt();
}
}
},"monitor");
monitorThread.start();
}
public void stop(){
monitorThread.interrupt();
}
}
如果代码改为
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class test1 {
public static void main(String[] args) throws InterruptedException {
TwoPhaseTermination tpt = new TwoPhaseTermination();
tpt.start();
Thread.sleep(3500);
tpt.stop();
}
}
@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination{
private Thread monitorThread;
public void start(){
monitorThread = new Thread(()->{
while(true){
Thread current = Thread.currentThread();
if(current.interrupted()){
log.debug("料理后事");
break;
}
try{
Thread.sleep(1000);
log.debug("执行监控记录");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"monitor");
monitorThread.start();
}
public void stop(){
monitorThread.interrupt();
}
}
如果将代码改为:
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class test1 {
public static void main(String[] args) throws InterruptedException {
TwoPhaseTermination tpt = new TwoPhaseTermination();
tpt.start();
Thread.sleep(3500);
tpt.stop();
}
}
@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination{
private Thread monitorThread;
public void start(){
monitorThread = new Thread(()->{
while(true){
Thread current = Thread.currentThread();
if(current.isInterrupted()){
log.debug("料理后事");
break;
}
try{
Thread.sleep(1000);
log.debug("执行监控记录");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"monitor");
monitorThread.start();
}
public void stop(){
monitorThread.interrupt();
}
}
1、isInterrupted + catch里面 interrupt ,正常退出
2、interrupted + catch里面 interrupt, 正常退出
3、isInterrupted,不能正常退出
4、interrupted, 不能正常退出
第一和第二种情况是因为interrupt会重置打断标记。
第三和第四种情况是因为interrupt打断正在睡眠的线程,会清除打断标记。所以无论这个判断方法是否清除打断标记,标记都是false。
打断 park 线程, 不会清空打断状态
private static void test3() throws InterruptedException {
Thread t1 = new Thread(() -> {
log.debug("park...");
LockSupport.park();
log.debug("unpark...");
log.debug("打断状态:{}", Thread.currentThread().isInterrupted());
}, "t1");
t1.start();
sleep(0.5);
t1.interrupt();
}
输出
21:11:52.795 [t1] c.TestInterrupt - park...
21:11:53.295 [t1] c.TestInterrupt - unpark...
21:11:53.295 [t1] c.TestInterrupt - 打断状态:true
如果打断标记已经是 true, 则 park 会失效
private static void test4() {
Thread t1 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
log.debug("park...");
LockSupport.park();
log.debug("打断状态:{}", Thread.currentThread().isInterrupted());
}
});
t1.start();
sleep(1);
t1.interrupt();
}
输出:
21:13:48.783 [Thread-0] c.TestInterrupt - park...
21:13:49.809 [Thread-0] c.TestInterrupt - 打断状态:true
21:13:49.812 [Thread-0] c.TestInterrupt - park...
21:13:49.813 [Thread-0] c.TestInterrupt - 打断状态:true
21:13:49.813 [Thread-0] c.TestInterrupt - park...
21:13:49.813 [Thread-0] c.TestInterrupt - 打断状态:true
21:13:49.813 [Thread-0] c.TestInterrupt - park...
21:13:49.813 [Thread-0] c.TestInterrupt - 打断状态:true
……