java 可以终止课程吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4379906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible to terminate a class?
提问by Skizit
I've got a program which runs multiple classes. I'd like a class to stop executing if a condition is true and NOT execute the rest of the code without killing the other classes. This class runs on it's own thread. is this do-able?
我有一个运行多个类的程序。如果条件为真,我希望一个类停止执行,并且在不杀死其他类的情况下不执行其余代码。这个类在它自己的线程上运行。这是可行的吗?
class A{
private void method{
if(condition == true){
//terminate class
}
}
}
回答by Joel
I'm not entirely sure what you're trying to do, but if the task runs in it's own thread, and so long as condition is a member variable of that class, then when you set condition to false you should be fine. Something like this:
我不完全确定您要做什么,但是如果任务在它自己的线程中运行,并且只要 condition 是该类的成员变量,那么当您将 condition 设置为 false 时,您应该没问题。像这样的东西:
public class Task implements Runnable {
private boolean stop = false;
private void stop() {
this.stop = stop;
}
@Override
public void run() {
while (!stop) {
// do it
}
}
public static void main(String[] args) {
Task task1 = new Task();
new Thread(task1).start();
Task task2 = new Task();
new Thread(task2).start();
// some time later
task1.stop();
// will stop task1, but not task 2
}
}
回答by darioo
If your class runs in its own thread, kill the thread. I don't think killing a classis doable in Java, whatever that would mean.
如果您的类在自己的线程中运行,请终止该线程。我不认为在 Java 中杀死一个类是可行的,不管这意味着什么。
回答by aioobe
I strongly advice you to reconsider your program flow and logical design. You should stop "gracefully" and not kill threads.
我强烈建议您重新考虑您的程序流程和逻辑设计。你应该“优雅地”停止而不是杀死线程。
However, to kill the current thread you could do
但是,要杀死当前线程,您可以这样做
Thread.currentThread().stop();
The stop
method is deprecated for a good reason though. Use with caution.
不过,该stop
方法已被弃用是有充分理由的。谨慎使用。
回答by ColinD
One option here might be to have your object implement Guava's Serviceinterface. It's specifically for services that are intended to be started and then at some point shut down. Specifically, you might want to make your class extend AbstractExecutionThreadService. This will handle making the service run on its own thread for you. Then you just need to start()
the service and stop()
the service when you need it to stop. You method can check isRunning()
to see if it should continue.
这里的一种选择可能是让您的对象实现Guava的Service接口。它专门用于旨在启动然后在某个时候关闭的服务。具体来说,您可能想让您的类扩展AbstractExecutionThreadService。这将为您处理使服务在其自己的线程上运行。然后你只需要start()
服务和stop()
服务在你需要的时候停止。您的方法可以检查isRunning()
它是否应该继续。
public class ClassA extends AbstractExecutionThreadService {
protected void run() {
while (isRunning()) {
...
}
}
// Can also start shutdown in another method
private void method() {
if (condition == true) {
triggerShutdown();
return;
}
}
}
All that said, I'm not too clear on exactly what you want to do or how you actually use this class (or classes), so it's hard to say how this will work for your situation.
综上所述,我不太清楚您到底想要做什么或您实际如何使用这个类(或多个类),因此很难说这将如何适用于您的情况。
回答by Spoike
If you want to stop the execution as soon as some condition is true then you always need to check that condition. It's a bit tedious to do though;
如果您想在某个条件成立时立即停止执行,那么您始终需要检查该条件。不过这样做有点乏味;
class A{
private boolean exit_me = false;
private void method{
if(!exit_me)
method1();
if(!exit_me)
method2();
if(!exit_me)
method3();
if(!exit_me)
method4();
// and so on
// will exit when all methods are executed or if exit_me is set
}
// implement method1, method2, method3, method4 etc...
}
回答by codymanix
A class cannot neither be executed, nor killed or terminated. Maybe you are talking about methods. Setting a volatile boolean variable may accomplish that.
一个类既不能被执行,也不能被杀死或终止。也许你在谈论方法。设置一个 volatile 布尔变量可以实现这一点。
class A{
public volatile boolean terminate_me;
private void method{
while (true)
{
if(terminate== true){
//terminate method
return;
}
}
}
}
If somebody set terminate_me to true, the method will return.
如果有人将 terminate_me 设置为 true,该方法将返回。