下面给出了一个子线程通过interrupt手段,来停止主线程的例子。
例:1.5.2_1
class ThreadMark_to_win extends Thread {
Thread mainT;
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("我是子线程,被打断");
return;
}
mainT.interrupt();
}
public void setMainThread(Thread t1) {
mainT = t1;
}
}
public class Test {
public static void main(String[] args) {
Thread mainT = Thread.currentThread();
ThreadMark_to_win st = new ThreadMark_to_win();
st.setMainThread(mainT);
st.start();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
System.out.println("我是主线程,被打断");
}
}
}
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/103099278
java中一个子线程如何通过interrupt手段来停止主线程
原文:https://www.cnblogs.com/xiaolongxia1922/p/14604717.html