1 19 20 package org.netbeans.editor.view.spi; 21 22 import javax.swing.text.View ; 23 24 43 44 public class ViewLayoutQueue { 45 46 private static ViewLayoutQueue defaultQueue; 47 48 private static SynchronousQueue synchronousQueue; 49 50 private Runnable [] taskArray; 51 52 private int startIndex; 53 54 private int endIndex; 55 56 private Thread worker; 57 58 61 public ViewLayoutQueue() { 62 taskArray = new Runnable [2]; } 64 65 68 public static ViewLayoutQueue getDefaultQueue() { 69 if (defaultQueue == null) { 70 defaultQueue = new ViewLayoutQueue(); 71 } 72 return defaultQueue; 73 } 74 75 public static ViewLayoutQueue getSynchronousQueue() { 76 if (synchronousQueue == null) { 77 synchronousQueue = new SynchronousQueue(); 78 } 79 return synchronousQueue; 80 } 81 82 public synchronized Thread getWorkerThread() { 83 ensureWorkerInited(); 84 85 return worker; 86 } 87 88 private void ensureWorkerInited() { 89 if (worker == null) { 90 worker = new LayoutThread(); 91 worker.start(); 92 } 93 } 94 95 98 public synchronized void addTask(Runnable task) { 99 if (task != null) { 100 101 ensureWorkerInited(); 103 104 if (startIndex == endIndex && taskArray[startIndex] != null) { Runnable [] newTaskArray = new Runnable [(taskArray.length << 1)]; 107 108 int afterStartIndexLength = taskArray.length - startIndex; 110 System.arraycopy(taskArray, endIndex, newTaskArray, 0, 111 afterStartIndexLength); 112 113 System.arraycopy(taskArray, 0, newTaskArray, afterStartIndexLength, 114 startIndex); 115 116 startIndex = 0; 117 endIndex = taskArray.length; taskArray = newTaskArray; 119 } 120 121 taskArray[endIndex] = task; 122 endIndex = (endIndex + 1) & (taskArray.length - 1); 123 124 notify(); 125 } 126 } 127 128 131 synchronized Runnable waitForTask() { 132 while (startIndex == endIndex && taskArray[startIndex] == null) { try { 134 wait(); 135 } catch (InterruptedException ie) { 136 return null; 137 } 138 } 139 140 Runnable task = taskArray[startIndex]; 141 taskArray[startIndex] = null; 142 int taskArrayLength = taskArray.length; 143 startIndex = (startIndex + 1) & (taskArrayLength - 1); 144 145 if (taskArrayLength >= 128) { int indexDiff = (endIndex - startIndex); 148 if (indexDiff >= 0) { if (indexDiff < (taskArrayLength / 8)) { 150 Runnable [] smallerTaskArray = new Runnable [(taskArrayLength / 4)]; 151 System.arraycopy(taskArray, startIndex, smallerTaskArray, 0, indexDiff); 152 taskArray = smallerTaskArray; 153 startIndex = 0; 154 endIndex = indexDiff; 155 } 156 157 } else { indexDiff = taskArrayLength + indexDiff; if (indexDiff < (taskArrayLength / 8)) { 160 Runnable [] smallerTaskArray = new Runnable [(taskArrayLength / 4)]; 161 System.arraycopy(taskArray, startIndex, smallerTaskArray, 0, 162 taskArrayLength - startIndex); 163 System.arraycopy(taskArray, 0, smallerTaskArray, 164 taskArrayLength - startIndex, endIndex); 165 taskArray = smallerTaskArray; 166 startIndex = 0; 167 endIndex = indexDiff; 168 } 169 } 170 } 171 172 return task; 173 } 174 175 178 class LayoutThread extends Thread { 179 180 LayoutThread() { 181 super("Text-Layout"); setPriority(Thread.MIN_PRIORITY); 183 } 184 185 public void run() { 186 Runnable task; 187 do { 188 task = waitForTask(); 189 if (task != null) { 190 task.run(); 191 } 192 } while (task != null); 193 } 194 195 196 } 197 198 203 private static class SynchronousQueue extends ViewLayoutQueue { 204 205 public void addTask(Runnable r) { 206 r.run(); 207 } 208 209 } 210 211 } 212 | Popular Tags |