2

I have a Java program that I need to kill a thread. It does not need to be killed gracefully, I just need the thread to end, as I am calling it to kill a bunch of threads as an action handler for JavaFX on window close.

Here is the program in question: https://github.com/Aashishkebab/Sorting-Simulator

Basically the program implements a bunch of sorting algorithms and allows the user to choose one, and then choose a block size. The program splits the sorting into blocks of the size that the user inputs, and then sorts all of these concurrently on separate threads.

However, closing the window causes the threads to keep sorting in the background. I need to be able to cause all of these operations to stop on window close (or pressing a kill button or whatever the case).

I am not worried about the safety of the data, or if null pointers occur, etc. I just want the program to truly exit.

Aashishkebab
  • 295
  • 3
  • 10
  • Can you make the threads handle interrupts, and cleanly shutdown, which should reduce the impact on tests? – jrtapsell Mar 13 '18 at 13:43
  • 3
    One way would be to use [daemon threads](https://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java) instead of regular threads. – BackSlash Mar 13 '18 at 13:44

4 Answers4

4

Just make the threads daemon threads. A daemon thread is one which does not prevent the JVM from exiting. This can be as simple as

Runnable mySortAlgorithm = ... ;
Thread thread = new Thread(mySortAlgorithm);
thread.setDaemon(true);
thread.start();

If you are using an executor to manage your threads, i.e. you have

Executor exec = ... ;
//...

Runnable mySortAlgorithm = ... ;
exec.execute(mySortAlgorithm);

you can create an executor that creates daemon threads, for example

Executor exec = Executors.newCachedThreadPool(runnable -> {
    Thread t = new Thread(runnable);
    t.setDaemon(true);
    return t ;
});

//...

Runnable mySortAlgorithm = ... ;
exec.execute(mySortAlgorithm);
James_D
  • 201,275
  • 16
  • 291
  • 322
2

If you use Executors to create an ExecutorService and submit() your Callable tasks to that, all task threads can be stopped with myExecutorService.shutdownNow()

If you want even better control over your threads, look into CompletableFuture.supplyAsync()

Jan Larsen
  • 831
  • 6
  • 13
  • Note that even a call to `shutdownNow()` requires that the running threads respect interrupt requests (for a sort algorithm, this would typically require they poll their interrupted status at regular intervals). – James_D Mar 13 '18 at 14:09
0

What about System.exit(myExitCode);.

But a cleaner solution would be to make your sorting algorithm cancellable. See here for example.

Also useful is this.

kerner1000
  • 3,382
  • 1
  • 37
  • 57
-1

It's not safe kill thread directly.

Recomended way is to use a flag that can notify to thread that is time to stop.

If you can access and modify code you can create a method named stop() in your thread class so when you need to kill process you can call myClass.stop().

For example:

public class myClass{

  private boolean keepAlive = true;

  public void run(){

     keepAlive = true;
     while(keepAlive){
       //do work
     }
  }

  public void stop(){
    keepAlive = false;
  }
}
David Geirola
  • 616
  • 3
  • 17
  • 2
    This is not guaranteed to work, assuming the intention is to call `stop()` from a thread other than the one on which `run()` is executing. You either need to mark `keepAlive` as `volatile`, or `synchronize` access to it, or use an `AtomicBoolean`. – James_D Mar 13 '18 at 13:52