1

java | 两阶段终止模式

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/12/java80/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java | 两阶段终止模式

2022-12-12java进阶多线程

2

在一个线程 T1 中如何优雅的终止线程 T2,这里的优雅值得是 T2 有一个处理各种事务的机会。

  • 调用 stop()
    • stop 会真正的杀死线程,如果,线程锁住了共享资源,则被锁住后,就没办法释放
  • System.exit(int)
    • 会把整个程序退出


no error
error
while
有没有被打断
执行监控记录
设置打断标记
package com.redisc;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test")
public class Test {

public static void main(String[] args) throws InterruptedException {
TwoPhaseTermination tpt = new TwoPhaseTermination();
tpt.start();

Thread.sleep(3000);
tpt.stop();
}
}

@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination {
private Thread monitor;

public void start() {
monitor = 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();
// 重新设置打断标记,因为 sleep 打断的时候,会把打断标记设置为 false,所以,这里要重置一下
current.interrupt();
}
}
});
monitor.start();
}

public void stop() {
monitor.interrupt();
}
}
23:06:58.965 [Thread-0] DEBUG c.TwoPhaseTermination - 执行监控
23:06:59.971 [Thread-0] DEBUG c.TwoPhaseTermination - 执行监控
java.lang.InterruptedException: sleep interrupted
at java.base/java.lang.Thread.sleep(Native Method)
at com.redisc.TwoPhaseTermination.lambda$start$0(Test.java:32)
at java.base/java.lang.Thread.run(Thread.java:832)
23:07:00.966 [Thread-0] DEBUG c.TwoPhaseTermination - 料理后事

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK