/Users/lyon/j4p/src/thread/ThreadusInterruptus.java

1    package thread; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.ActionListener; 
6     
7    abstract class RunButton extends Button 
8            implements Runnable, ActionListener { 
9        RunButton(String label) { 
10           super(label); 
11           addActionListener(this); 
12       } 
13    
14       Thread t = null; 
15    
16       public void actionPerformed(ActionEvent e) { 
17           t = new Thread(this); 
18           t.start(); 
19       } 
20   } 
21    
22   /** 
23    *   example of one button interrupting another button. 
24    */ 
25   public class ThreadusInterruptus extends Frame { 
26       static RunButton cancelButton = new RunButton("cancel") { 
27           public void run() { 
28               try { 
29                   Thread.sleep(2000); 
30                   System.out.println("cancel!"); 
31               } catch (InterruptedException ie) { 
32                   System.out.println("I canceled my cancel!"); 
33               } 
34           } 
35       }; 
36    
37    
38       public static void main(String args[]) { 
39           Frame f = new Frame(); 
40           f.setSize(200, 200); 
41           f.setLayout(new FlowLayout()); 
42           f.add(new RunButton("ok") { 
43               public void run() { 
44                   cancelButton.t.interrupt(); 
45                   System.out.println("dsfoirsojkgrnior"); 
46               } 
47           }); 
48           f.add(cancelButton); 
49           f.show(); 
50       } 
51   } 
52    
53