小编mad*_*dhu的帖子

中断java中正常运行的线程

我试图中断正常运行的线程(未处于 sleep() 或 wait() 状态)。

在网络中浏览时,我知道中断正常运行的线程只会将标志设置为 true 并继续该过程。

代码片段是

one.java

......
......
actionperformedmethod {

if (actionCmd.equals("cancel")) {
    try {
        r1.stop();  // to two.java
    } catch (InterruptedException ex) {
        ....
        ....
    }
}
}
Run Code Online (Sandbox Code Playgroud)

two.java

.....
.....
stop method() throws InterruptedException{
        if(!(t.isInterrupted())){
            t.interrupt();
            throw new InterruptedException();
        }
}
Run Code Online (Sandbox Code Playgroud)

从 Two.java 当我抛出 InterruptedException 时,我可以在 one.java 处获取异常块,但是之后如何停止线程,因为即使在该线程似乎继续正常过程之后。

我对线程概念不熟悉,请帮忙..

java multithreading interrupted-exception

1
推荐指数
1
解决办法
6797
查看次数