Java 我怎样才能安全地停止我的;类实现可运行的";?

Java 我怎样才能安全地停止我的;类实现可运行的";?,java,multithreading,class,runnable,Java,Multithreading,Class,Runnable,委员会建议这样做: 通过将小程序的停止和运行方法替换为以下方法,可以避免使用Thread.stop: 对于类blinker implements Runnable,有没有办法做同样的事情 因为您必须使用blinker thisClass=this或类似的代码,(blinker==thisClass)是否总是计算为true 或者该代码是否足够: class blinker implements Runnable { boolean stop = false; @override

委员会建议这样做:

通过将小程序的停止和运行方法替换为以下方法,可以避免使用Thread.stop:

对于
类blinker implements Runnable
,有没有办法做同样的事情

因为您必须使用
blinker thisClass=this
或类似的代码,
(blinker==thisClass)
是否总是计算为true

或者该代码是否足够:

class blinker implements Runnable {
    boolean stop = false;

    @override
    public void run() {

        while (!Thread.currentThread().isInterrupted()) {

            // code
            // ...

            if (stop) { Thread.currentThread().interrupt(); }

            // ...

        }
    }
}
你可以这样做:

class Blinker implements Runnable {
    Runnable blinker = this;

    public void stop() {
        blinker = null;
    }

    public void run() {
        while(blinker == this) {

        }
    }
}
但这是毫无意义的。我认为您没有理解文档试图传达的要点,即不要使用无限循环来保持线程的活动状态,使用
Thread#stop()
来终止线程。相反,使用一个条件,然后当您想要结束使线程保持活动状态的循环时,将其设置为false

while(!stop) {

}

您不需要经常检查
Thread#isInterrupted()
来保持线程的活动状态

while(!stop) {

}
那就好了。您也不应该从线程内部中断线程。中断的目的是结束停止线程的任务。这些任务围绕在捕获
中断异常的
try/catch
中。其他线程通常负责中断


文档中提到的是允许线程优雅地消亡

在第一个示例中,
run()
方法通过无限循环处理:
while(true)
。停止线程的唯一方法是强制某种类型的停止,例如usong
thread#stop

public void run() {
    while (true) {
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e){
        }
        repaint();
    }
}
但不建议使用
线程#停止
。相反,循环应该依赖于另一个线程(或当前线程)可以设置为
true
false的
boolean

private volatile boolean running;

public void stop() {
    running = false;
}

public void run() {
    while (running) {
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e){
        }
        repaint();
    }
}
他们没有使用
运行
布尔值,而是使用
blinker==thisThread
,然后在想要结束循环时更改
blinker
的值:

private volatile Thread blinker;

public void stop() {
    blinker = null;
}

public void run() {
    Thread thisThread = Thread.currentThread();
    while (blinker == thisThread) {
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e){
        }
        repaint();
    }
}
你可以这样做:

class Blinker implements Runnable {
    Runnable blinker = this;

    public void stop() {
        blinker = null;
    }

    public void run() {
        while(blinker == this) {

        }
    }
}
但这是毫无意义的。我认为您没有理解文档试图传达的要点,即不要使用无限循环来保持线程的活动状态,使用
Thread#stop()
来终止线程。相反,使用一个条件,然后当您想要结束使线程保持活动状态的循环时,将其设置为false

while(!stop) {

}

您不需要经常检查
Thread#isInterrupted()
来保持线程的活动状态

while(!stop) {

}
那就好了。您也不应该从线程内部中断线程。中断的目的是结束停止线程的任务。这些任务围绕在捕获
中断异常的
try/catch
中。其他线程通常负责中断


文档中提到的是允许线程优雅地消亡

在第一个示例中,
run()
方法通过无限循环处理:
while(true)
。停止线程的唯一方法是强制某种类型的停止,例如usong
thread#stop

public void run() {
    while (true) {
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e){
        }
        repaint();
    }
}
但不建议使用
线程#停止
。相反,循环应该依赖于另一个线程(或当前线程)可以设置为
true
false的
boolean

private volatile boolean running;

public void stop() {
    running = false;
}

public void run() {
    while (running) {
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e){
        }
        repaint();
    }
}
他们没有使用
运行
布尔值,而是使用
blinker==thisThread
,然后在想要结束循环时更改
blinker
的值:

private volatile Thread blinker;

public void stop() {
    blinker = null;
}

public void run() {
    Thread thisThread = Thread.currentThread();
    while (blinker == thisThread) {
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e){
        }
        repaint();
    }
}

我不要相信甲骨文的建议。通常的方法是
Thread.interrupt()
。不,
Runnable
没有等价物,只需使用
Thread.interrupt()
Future.cancel(true)
,视情况而定。@markspace我刚刚读过。这似乎是个糟糕的主意。仅仅因为它是Oracle并不意味着他们不能发布考虑不周的代码。我认为这里的主要思想是“不要调用Thread.stop()”。其他的都是乱七八糟的。OP:使用Thread.interrupt()。因此(!Thread.currentThread().isInterrupted()){}
Thread.currentThread()。该页的下一节将介绍中断。这一部分解释了停止的替代方案,虽然没有用最好的方式解释,但知道它是非常重要的。我同意文件写得很糟糕。。不要相信甲骨文的建议。通常的方法是
Thread.interrupt()
。不,
Runnable
没有等价物,只需使用
Thread.interrupt()
Future.cancel(true)
,视情况而定。@markspace我刚刚读过。这似乎是个糟糕的主意。仅仅因为它是Oracle并不意味着他们不能发布考虑不周的代码。我认为这里的主要思想是“不要调用Thread.stop()”。其他的都是乱七八糟的。OP:使用Thread.interrupt()。因此(!Thread.currentThread().isInterrupted()){}
Thread.currentThread()。该页的下一节将介绍中断。这一部分解释了停止的替代方案,虽然没有用最好的方式解释,但知道它是非常重要的。我确实同意文档写得很糟糕。这实际上并没有回答OP的问题“如果
blinker
是一个实现
可运行的
类的实例怎么办?”(意译)。你为什么要吞下
中断异常
?@dimo414它是直接从文档中提取的代码。我只是做了一些修改来说明代码的用途。在开始的时候,例外被吞没了,我想与文档保持一致。这实际上并没有回答OP关于“w”的问题