开发者

Possible to terminate a class?

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 ow开发者_开发技巧n thread. is this do-able?

 class A{
    private void method{
           if(condition == true){
             //terminate class
            }
    }
}


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:

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
    }
}


If your class runs in its own thread, kill the thread. I don't think killing a class is doable in Java, whatever that would mean.


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.


One option here might be to have your object implement Guava's Service interface. 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.

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.


A class cannot neither be executed, nor killed or terminated. Maybe you are talking about methods. Setting a volatile boolean variable may accomplish that.

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.


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...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜