-1

I've got a Main() that gets executed as as Thread on a schedule. If an exception happens (e.printStackTrace), the threads aborts and waits until the next starts.

I want to be able to abort the thread without throwing an exeption. Inside the Main()-Class it's fairly easy, just "return false", and the thread ends.

But when I'm inside a Class/Object of a Class, I can't do that. How can I abort/finish the Main from within a subclass?

3 Answers3

0

Do you want to abort the Thread, or do you want to shutdown the VM?

If you want to stop the VM, try Runtime.getRuntime().exit(0);

Stopping an arbitrary running thread is generally unsafe, try reading: http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

to better explain the situation.

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
JohnnyO
  • 3,018
  • 18
  • 30
0

If I understand you right, you don't want to shutdown whole program, just some thread?

You need to return from all submethods you get in. Can you show how your method run() looks inside your threads?

For example: if you return from some function (doSomething()) you need to return from method run():

@Override
public void run() {
    while (true) {
        doSomething(); // - your main function
    }
}

Just check some condition. If you want to exit set value boolean exiting = true; and check it inside your loop: if (exiting) return;

The correct way to close any thread is just to return from method run

alaster
  • 3,821
  • 3
  • 24
  • 32
-1

You could try System.exit(0).

Let us know if that works.