1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* * @(#)LayoutQueue.java 1.7 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.text; import java.util.Vector; /** * A queue of text layout tasks. * * @author Timothy Prinzing * @version 1.7 11/17/05 * @see AsyncBoxView * @since 1.3 */ public class LayoutQueue { Vector tasks; Thread worker; static LayoutQueue defaultQueue; /** * Construct a layout queue. */ public LayoutQueue() { tasks = new Vector(); } /** * Fetch the default layout queue. */ public static LayoutQueue getDefaultQueue() { if (defaultQueue == null) { defaultQueue = new LayoutQueue(); } return defaultQueue; } /** * Set the default layout queue. * * @param q the new queue. */ public static void setDefaultQueue(LayoutQueue q) { defaultQueue = q; } /** * Add a task that is not needed immediately because * the results are not believed to be visible. */ public synchronized void addTask(Runnable task) { if (worker == null) { worker = new LayoutThread(); worker.start(); } tasks.addElement(task); notifyAll(); } /** * Used by the worker thread to get a new task to execute */ protected synchronized Runnable waitForWork() { while (tasks.size() == 0) { try { wait(); } catch (InterruptedException ie) { return null; } } Runnable work = (Runnable) tasks.firstElement(); tasks.removeElementAt(0); return work; } /** * low priority thread to perform layout work forever */ class LayoutThread extends Thread { LayoutThread() { super("text-layout"); setPriority(Thread.MIN_PRIORITY); } public void run() { Runnable work; do { work = waitForWork(); if (work != null) { work.run(); } } while (work != null); } } }