< prev index next >

src/java.desktop/share/classes/javax/swing/text/LayoutQueue.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2008, 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


  33  * @author  Timothy Prinzing
  34  * @see     AsyncBoxView
  35  * @since   1.3
  36  */
  37 public 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      */
  54     public static LayoutQueue getDefaultQueue() {
  55         AppContext ac = AppContext.getAppContext();
  56         synchronized (DEFAULT_QUEUE) {
  57             LayoutQueue defaultQueue = (LayoutQueue) ac.get(DEFAULT_QUEUE);
  58             if (defaultQueue == null) {
  59                 defaultQueue = new LayoutQueue();
  60                 ac.put(DEFAULT_QUEUE, defaultQueue);
  61             }
  62             return defaultQueue;
  63         }
  64     }
  65 
  66     /**
  67      * Set the default layout queue.
  68      *
  69      * @param q the new queue.
  70      */
  71     public static void setDefaultQueue(LayoutQueue q) {
  72         synchronized (DEFAULT_QUEUE) {
  73             AppContext.getAppContext().put(DEFAULT_QUEUE, q);
  74         }
  75     }
  76 
  77     /**
  78      * Add a task that is not needed immediately because
  79      * the results are not believed to be visible.

  80      */
  81     public synchronized void addTask(Runnable task) {
  82         if (worker == null) {
  83             worker = new LayoutThread();
  84             worker.start();
  85         }
  86         tasks.addElement(task);
  87         notifyAll();
  88     }
  89 
  90     /**
  91      * Used by the worker thread to get a new task to execute

  92      */
  93     protected synchronized Runnable waitForWork() {
  94         while (tasks.size() == 0) {
  95             try {
  96                 wait();
  97             } catch (InterruptedException ie) {
  98                 return null;
  99             }
 100         }
 101         Runnable work = tasks.firstElement();
 102         tasks.removeElementAt(0);
 103         return work;
 104     }
 105 
 106     /**
 107      * low priority thread to perform layout work forever
 108      */
 109     class LayoutThread extends Thread {
 110 
 111         LayoutThread() {
   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


  33  * @author  Timothy Prinzing
  34  * @see     AsyncBoxView
  35  * @since   1.3
  36  */
  37 public 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             worker = new LayoutThread();
  86             worker.start();
  87         }
  88         tasks.addElement(task);
  89         notifyAll();
  90     }
  91 
  92     /**
  93      * Used by the worker thread to get a new task to execute.
  94      * @return a task from the queue
  95      */
  96     protected synchronized Runnable waitForWork() {
  97         while (tasks.size() == 0) {
  98             try {
  99                 wait();
 100             } catch (InterruptedException ie) {
 101                 return null;
 102             }
 103         }
 104         Runnable work = tasks.firstElement();
 105         tasks.removeElementAt(0);
 106         return work;
 107     }
 108 
 109     /**
 110      * low priority thread to perform layout work forever
 111      */
 112     class LayoutThread extends Thread {
 113 
 114         LayoutThread() {
< prev index next >