Saturday, September 6, 2014

14. Interrupting Threads

Java Multithreading: Interrupting Threads 
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.

Code For This Tutorial


import java.util.Random;

public class App {

      public static void main(String[] args) throws InterruptedException {
            System.out.println("Starting.");

            Thread t1 = new Thread(new Runnable() {
                  public void run() {
                        Random ran = new Random();

                        for (int i = 0; i < 1E8; i++) {
                              /*
                                if(Thread.currentThread().isInterrupted()){
                                System.out.println("Interrupted!"); break; }
                               */
                              try {
                                    Thread.sleep(1);
                              } catch (InterruptedException e) {
                                    System.out.println("Interrupted!");
                                    break;
                              }

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

                  }
            });

            t1.start();

            Thread.sleep(500);

            t1.interrupt();

            t1.join();

            System.out.println("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.");
    }

}

Output:
Starting.
Interrupted!

Finished.

1 comment:

  1. Happy to found this blog. Good Post!. It was so good to read and useful to improve my knowledge as updated one, keep blogging. Hibernate Training in Electronic City
    Java Training in Electronic City

    ReplyDelete