1 3 5 22 23 package edu.neu.ccs.jmk.awt; 24 25 import java.util.Vector ; 26 27 34 public class RunQueue 35 implements Runnable 36 { 37 private final Vector queue = new Vector (); 39 40 private volatile boolean interrupt = false; 42 43 private volatile boolean poisoned = false; 45 46 51 public void setPoisoned(boolean poisoned) { 52 this.poisoned = poisoned; 53 } 54 55 59 public synchronized void add(Runnable task) { 60 queue.addElement(task); 61 notify(); 62 } 63 64 67 public synchronized void removeAll() { 68 interrupt = true; queue.removeAllElements(); } 71 72 75 public void run() { 76 Vector q; 77 while (!poisoned) { 78 try { 79 synchronized (this) { 80 while (queue.isEmpty()) 81 wait(); 82 q = (Vector )queue.clone(); queue.removeAllElements(); 84 interrupt = false; 85 } 86 } 87 catch (InterruptedException ie) { 88 continue; } 90 for (int i = 0; i < q.size() && !interrupt; i++) { 92 Runnable task = (Runnable )q.elementAt(i); 93 try { 94 task.run(); 95 } 96 catch (Throwable t) { 97 System.err.println("Internal error: " + t.getMessage()); 98 t.printStackTrace(); 99 } 100 } 101 q = null; } 103 } 104 } 105 | Popular Tags |