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