1 /* 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text; 26 27 import java.util.Vector; 28 import sun.awt.AppContext; 29 import sun.misc.ManagedLocalsThread; 30 31 /** 32 * A queue of text layout tasks. 33 * 34 * @author Timothy Prinzing 35 * @see AsyncBoxView 36 * @since 1.3 37 */ 38 public class LayoutQueue { 39 40 private static final Object DEFAULT_QUEUE = new Object(); 41 42 private Vector<Runnable> tasks; 43 private Thread worker; 44 45 /** 46 * Construct a layout queue. 47 */ 48 public LayoutQueue() { 49 tasks = new Vector<Runnable>(); 50 } 51 52 /** 53 * Fetch the default layout queue. 54 * @return the default layout queue 55 */ 56 public static LayoutQueue getDefaultQueue() { 57 AppContext ac = AppContext.getAppContext(); 58 synchronized (DEFAULT_QUEUE) { 59 LayoutQueue defaultQueue = (LayoutQueue) ac.get(DEFAULT_QUEUE); 60 if (defaultQueue == null) { 61 defaultQueue = new LayoutQueue(); 62 ac.put(DEFAULT_QUEUE, defaultQueue); 63 } 64 return defaultQueue; 65 } 66 } 67 68 /** 69 * Set the default layout queue. 70 * 71 * @param q the new queue. 72 */ 73 public static void setDefaultQueue(LayoutQueue q) { 74 synchronized (DEFAULT_QUEUE) { 75 AppContext.getAppContext().put(DEFAULT_QUEUE, q); 76 } 77 } 78 79 /** 80 * Add a task that is not needed immediately because 81 * the results are not believed to be visible. 82 * @param task the task to add to the queue 83 */ 84 public synchronized void addTask(Runnable task) { 85 if (worker == null) { 86 Runnable workerRunnable = () -> { 87 Runnable work; 88 do { 89 work = waitForWork(); 90 if (work != null) { 91 work.run(); 92 } 93 } while (work != null); 94 }; 95 worker = new ManagedLocalsThread(workerRunnable, "text-layout"); 96 worker.setPriority(Thread.MIN_PRIORITY); 97 worker.start(); 98 } 99 tasks.addElement(task); 100 notifyAll(); 101 } 102 103 /** 104 * Used by the worker thread to get a new task to execute. 105 * @return a task from the queue 106 */ 107 protected synchronized Runnable waitForWork() { 108 while (tasks.size() == 0) { 109 try { 110 wait(); 111 } catch (InterruptedException ie) { 112 return null; 113 } 114 } 115 Runnable work = tasks.firstElement(); 116 tasks.removeElementAt(0); 117 return work; 118 } 119 }