@@ -0,0 +1,1522 @@
+package thinkInjava.c14;%0A%0A//: Interrupt.java%0A//The alternative approach to using stop()%0A//when a thread is blocked%0Aimport java.applet.Applet;%0Aimport java.awt.BorderLayout;%0Aimport java.awt.Button;%0Aimport java.awt.Frame;%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0Aimport java.awt.event.WindowAdapter;%0Aimport java.awt.event.WindowEvent;%0A%0Aclass Blocked extends Thread %7B%0A%09@Override%0A%09public synchronized void run() %7B%0A%09%09try %7B%0A%09%09%09wait(); // Blocks%0A%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09System.out.println(%22InterruptedException%22);%0A%09%09%7D%0A%09%09System.out.println(%22Exiting run()%22);%0A%09%7D%0A%7D%0A%0Apublic class Interrupt extends Applet %7B%0A%09private Button interrupt = new Button(%22Interrupt%22);%0A%09private Blocked blocked = new Blocked();%0A%0A%09@Override%0A%09public void init() %7B%0A%09%09add(interrupt);%0A%09%09interrupt.addActionListener(new ActionListener() %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%09%09%09%09System.out.println(%22Button pressed%22);%0A%09%09%09%09if (blocked == null) %7B%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09%09%09%09Thread remove = blocked;%0A%09%09%09%09blocked = null; // to release it%0A%09%09%09%09remove.interrupt();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09blocked.start();%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) %7B%0A%09%09Interrupt applet = new Interrupt();%0A%09%09Frame aFrame = new Frame(%22Interrupt%22);%0A%09%09aFrame.addWindowListener(new WindowAdapter() %7B%0A%09%09%09@Override%0A%09%09%09public void windowClosing(WindowEvent e) %7B%0A%09%09%09%09System.exit(0);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09aFrame.add(applet, BorderLayout.CENTER);%0A%09%09aFrame.setSize(200, 100);%0A%09%09applet.init();%0A%09%09applet.start();%0A%09%09aFrame.setVisible(true);%0A%09%7D%0A%7D // /:~%0A%0A

add interrupt