Java Multithreading: Interrupting Threads (Video Tutorial Part 14)

What exactly are those pesky InterruptedExceptions? In this video I show you how to interrupt running threads in Java using the built-in thread interruption mechanism.

After starting the video, click the maximise button to make it fullscreen so you can see the code!



Code For This Tutorial




Starting.
Interrupted!
Finished.



 
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class App {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Starting.");

        ExecutorService exec = Executors.newCachedThreadPool();

        Future<?> fu = exec.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Random ran = new Random();

                for (int i = 0; i < 1E8; i++) {

                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("Interrupted!");
                        break;
                    }

                    Math.sin(ran.nextDouble());
                }
                return null;
            }

        });
        
        exec.shutdown();
        
        
        Thread.sleep(500);
        
        exec.shutdownNow();
        //fu.cancel(true);
        
        exec.awaitTermination(1, TimeUnit.DAYS);
        
        System.out.println("Finished.");
    }

}