4

I have an executor service, and I want to cancel/interrupt the execution of some of the threads.

For example: Below is my Thread class which prints the thread name after some interval infinitely.

public class MyThread implements Runnable {
String name;

public MyThread(String name) {
    this.name = name;
}

@Override
public void run() {
    try {
        System.out.println("Thread "+ name + " is running");
        sleep(500);
    }catch (InterruptedException e){
        System.out.println("got the interrupted signal");
    }
}
}

Now I'll create multiple threads by giving them name, so that later on I can stop a particular thread with it's name.

As shown below, I am creating 4 threads and want to stop the execution of 2 threads named foo and bar.

public class ThreadTest {

public static void main(String[] args) {
    ExecutorService executorService = Executors.newCachedThreadPool();
    MyThread amit = new MyThread("foo");
    MyThread k = new MyThread("bar");
    MyThread blr = new MyThread("tel-aviv");
    MyThread india = new MyThread("israel");
    executorService.submit(foo);
    executorService.submit(bar);
    executorService.submit(tel-aviv);
    executorService.submit(israel);
}
}
Amit
  • 30,756
  • 6
  • 57
  • 88
  • 3
    Hint: `executorService.submit` gives you back a `Future>`. – Andy Turner May 19 '17 at 08:00
  • @AndyTurner , i know it gives a `Future` but can you let me know how can i use it in my case to retrive the threads with name `amit` and interrupt its execution ? – Amit May 19 '17 at 08:01
  • Your `MyThread` doesn't need to extend `Thread`. Just implement `Runnable`. – Andy Turner May 19 '17 at 08:02
  • 1
    Just keep the futures in a Map> by name so you can look them up by whatever name you like. – Erwin Bolwidt May 19 '17 at 08:03
  • @AmitK your `MyThread`s are not actually being run on threads with those names. They're not being run directly as threads, they are run on the `ExecutorService`'s threads. So, you need to keep a mapping of name to `Future`, and then cancel the future when you want to. – Andy Turner May 19 '17 at 08:04
  • @AndyTurner Thanks for the hint, i'll try this and update. – Amit May 19 '17 at 08:06
  • @AndyTurner is it possible to give my own name to the thread to make the logging and debugging easy ? – Amit May 19 '17 at 08:43

1 Answers1

10

Your MyThreads are not actually being run on threads with those names. They're not being run directly as threads, they are run on the ExecutorService's threads.

So, you need to keep a mapping of name to Future, and then cancel the future when you want to.

Map<String, Future<?>> map = new HashMap<>();
map.put("amit", executorService.submit(amit));
map.put("k", executorService.submit(k));
// ... etc

Then, to cancel amit:

map.get("amit").cancel(true);

Of course, you could simply have kept explicit variables:

Future<?> amitFuture = executorService.submit(amit);
amitFuture.cancel(true);

but this might be unwieldy if you have a lot of variables.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Also is it possible to give me own name to the thread to make the logging and debugging easy ? – Amit May 19 '17 at 08:16