1 7 package javax.swing.text; 8 9 import java.util.Vector ; 10 11 19 public class LayoutQueue { 20 21 Vector tasks; 22 Thread worker; 23 24 static LayoutQueue defaultQueue; 25 26 29 public LayoutQueue() { 30 tasks = new Vector (); 31 } 32 33 36 public static LayoutQueue getDefaultQueue() { 37 if (defaultQueue == null) { 38 defaultQueue = new LayoutQueue (); 39 } 40 return defaultQueue; 41 } 42 43 48 public static void setDefaultQueue(LayoutQueue q) { 49 defaultQueue = q; 50 } 51 52 56 public synchronized void addTask(Runnable task) { 57 if (worker == null) { 58 worker = new LayoutThread(); 59 worker.start(); 60 } 61 tasks.addElement(task); 62 notifyAll(); 63 } 64 65 68 protected synchronized Runnable waitForWork() { 69 while (tasks.size() == 0) { 70 try { 71 wait(); 72 } catch (InterruptedException ie) { 73 return null; 74 } 75 } 76 Runnable work = (Runnable ) tasks.firstElement(); 77 tasks.removeElementAt(0); 78 return work; 79 } 80 81 84 class LayoutThread extends Thread { 85 86 LayoutThread() { 87 super("text-layout"); 88 setPriority(Thread.MIN_PRIORITY); 89 } 90 91 public void run() { 92 Runnable work; 93 do { 94 work = waitForWork(); 95 if (work != null) { 96 work.run(); 97 } 98 } while (work != null); 99 } 100 101 102 } 103 104 } 105 | Popular Tags |