_Csandeep

InterruptedException

Feb 19th, 2013 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. package miscellaneous;
  2.  
  3. public class ThreadDemo {
  4.         public static void main (String args[]) {
  5.                 final Thread t = new Thread(new Runnable() {
  6.                     public void run () {
  7.                                 new DoSomething().doSomething();
  8.                         }
  9.                 });
  10.                 t.start();
  11.                 Thread t2 = new Thread(new Runnable () {
  12.                         public void run () {
  13.                                 try {
  14.                                         Thread.sleep(2000);
  15.                                         t.interrupt();
  16.                                 } catch (InterruptedException e) {
  17.                                         e.printStackTrace();
  18.                                 }
  19.                         }
  20.                 });
  21.                 t2.start();
  22.         }
  23.         static class DoSomething {
  24.                 public synchronized void doSomething () {
  25.                         try {
  26.                                 wait();
  27.                         } catch (InterruptedException e) {
  28.                                 System.out.println("Interrupted :(");
  29.                         }
  30.                 }
  31.         }
  32. }
Add Comment
Please, Sign In to add comment