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