KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > LayoutQueue


1 /*
2  * @(#)LayoutQueue.java 1.6 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.util.Vector JavaDoc;
10
11 /**
12  * A queue of text layout tasks.
13  *
14  * @author Timothy Prinzing
15  * @version 1.6 12/19/03
16  * @see AsyncBoxView
17  * @since 1.3
18  */

19 public class LayoutQueue {
20
21     Vector JavaDoc tasks;
22     Thread JavaDoc worker;
23
24     static LayoutQueue JavaDoc defaultQueue;
25
26     /**
27      * Construct a layout queue.
28      */

29     public LayoutQueue() {
30     tasks = new Vector JavaDoc();
31     }
32
33     /**
34      * Fetch the default layout queue.
35      */

36     public static LayoutQueue JavaDoc getDefaultQueue() {
37     if (defaultQueue == null) {
38         defaultQueue = new LayoutQueue JavaDoc();
39     }
40     return defaultQueue;
41     }
42
43     /**
44      * Set the default layout queue.
45      *
46      * @param q the new queue.
47      */

48     public static void setDefaultQueue(LayoutQueue JavaDoc q) {
49     defaultQueue = q;
50     }
51
52     /**
53      * Add a task that is not needed immediately because
54      * the results are not believed to be visible.
55      */

56     public synchronized void addTask(Runnable JavaDoc task) {
57     if (worker == null) {
58         worker = new LayoutThread();
59         worker.start();
60     }
61     tasks.addElement(task);
62     notifyAll();
63     }
64
65     /**
66      * Used by the worker thread to get a new task to execute
67      */

68     protected synchronized Runnable JavaDoc waitForWork() {
69     while (tasks.size() == 0) {
70         try {
71         wait();
72         } catch (InterruptedException JavaDoc ie) {
73         return null;
74         }
75     }
76     Runnable JavaDoc work = (Runnable JavaDoc) tasks.firstElement();
77     tasks.removeElementAt(0);
78     return work;
79     }
80
81     /**
82      * low priority thread to perform layout work forever
83      */

84     class LayoutThread extends Thread JavaDoc {
85     
86     LayoutThread() {
87         super("text-layout");
88         setPriority(Thread.MIN_PRIORITY);
89     }
90     
91         public void run() {
92         Runnable JavaDoc 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