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 */
25package javax.swing.text;
26
27import java.util.Vector;
28import sun.awt.AppContext;
29
30/**
31 * A queue of text layout tasks.
32 *
33 * @author  Timothy Prinzing
34 * @see     AsyncBoxView
35 * @since   1.3
36 */
37public class LayoutQueue {
38
39    private static final Object DEFAULT_QUEUE = new Object();
40
41    private Vector<Runnable> tasks;
42    private Thread worker;
43
44    /**
45     * Construct a layout queue.
46     */
47    public LayoutQueue() {
48        tasks = new Vector<Runnable>();
49    }
50
51    /**
52     * Fetch the default layout queue.
53     * @return the default layout queue
54     */
55    public static LayoutQueue getDefaultQueue() {
56        AppContext ac = AppContext.getAppContext();
57        synchronized (DEFAULT_QUEUE) {
58            LayoutQueue defaultQueue = (LayoutQueue) ac.get(DEFAULT_QUEUE);
59            if (defaultQueue == null) {
60                defaultQueue = new LayoutQueue();
61                ac.put(DEFAULT_QUEUE, defaultQueue);
62            }
63            return defaultQueue;
64        }
65    }
66
67    /**
68     * Set the default layout queue.
69     *
70     * @param q the new queue.
71     */
72    public static void setDefaultQueue(LayoutQueue q) {
73        synchronized (DEFAULT_QUEUE) {
74            AppContext.getAppContext().put(DEFAULT_QUEUE, q);
75        }
76    }
77
78    /**
79     * Add a task that is not needed immediately because
80     * the results are not believed to be visible.
81     * @param task the task to add to the queue
82     */
83    public synchronized void addTask(Runnable task) {
84        if (worker == null) {
85            Runnable workerRunnable = () -> {
86                Runnable work;
87                do {
88                    work = waitForWork();
89                    if (work != null) {
90                        work.run();
91                    }
92                } while (work != null);
93            };
94            worker = new Thread(null, workerRunnable, "text-layout", 0, false);
95            worker.setPriority(Thread.MIN_PRIORITY);
96            worker.start();
97        }
98        tasks.addElement(task);
99        notifyAll();
100    }
101
102    /**
103     * Used by the worker thread to get a new task to execute.
104     * @return a task from the queue
105     */
106    protected synchronized Runnable waitForWork() {
107        while (tasks.size() == 0) {
108            try {
109                wait();
110            } catch (InterruptedException ie) {
111                return null;
112            }
113        }
114        Runnable work = tasks.firstElement();
115        tasks.removeElementAt(0);
116        return work;
117    }
118}
119