1 21 22 package org.armedbear.j; 23 24 import java.util.Vector ; 25 import javax.swing.SwingUtilities ; 26 27 public class IdleThread extends Thread 28 { 29 private Vector tasks = new Vector (); 30 31 private static IdleThread idleThread; 32 33 private IdleThread() 34 { 35 super("idle"); 36 } 37 38 public static synchronized IdleThread getInstance() 39 { 40 if (idleThread == null) 41 startIdleThread(); 42 return idleThread; 43 } 44 45 public static synchronized void startIdleThread() 46 { 47 if (idleThread == null) { 48 idleThread = new IdleThread(); 49 idleThread.init(); 50 idleThread.setPriority(Thread.MIN_PRIORITY); 51 idleThread.setDaemon(true); 52 idleThread.start(); 53 } 54 } 55 56 public static synchronized void runFollowContextTask(IdleThreadTask task) 57 { 58 if (followContextTask == null) { 59 followContextTask = task; 60 followContextTask.run(); 61 if (idleThread != null) 62 idleThread.addTask(followContextTask); 63 } 64 } 65 66 public static synchronized void killFollowContextTask() 67 { 68 if (followContextTask != null) { 69 if (idleThread != null) 70 idleThread.removeTask(followContextTask); 71 followContextTask = null; 72 } 73 } 74 75 private synchronized void init() 76 { 77 addTask(parseBuffersTask); 78 addTask(updateHorizontalScrollBarsTask); 79 addTask(updateSidebarTask); 80 addTask(gcTask); 81 addTask(autosaveTask); 82 addTask(saveStateTask); 83 addTask(tagCurrentDirectoryTask); 84 if (Editor.isDebugEnabled()) 85 addListThreadsTask(); 86 } 87 88 private synchronized IdleThreadTask getTask(int index) 89 { 90 if (index < 0 || index >= tasks.size()) 91 return null; 92 return (IdleThreadTask) tasks.get(index); 93 } 94 95 private synchronized void addTask(IdleThreadTask task) 96 { 97 tasks.add(task); 98 } 99 100 public synchronized void maybeAddTask(IdleThreadTask task) 101 { 102 for (int i = tasks.size(); i-- > 0;) { 103 if (tasks.get(i) == task) 104 return; } 106 tasks.add(task); 107 } 108 109 private synchronized void removeTask(IdleThreadTask task) 110 { 111 tasks.remove(task); 112 } 113 114 public void run() 115 { 116 while (true) { 117 try { 118 Thread.sleep(500); 119 } 120 catch (InterruptedException e) {} 121 final long lastEventMillis = Dispatcher.getLastEventMillis(); 122 final long idle = System.currentTimeMillis() - lastEventMillis; 123 if (idle > 500) { 124 int i = 0; 125 IdleThreadTask task; 126 while ((task = getTask(i++)) != null) { 127 if (task.getIdle() == 0) 128 ; 129 else if (idle > task.getIdle()) 130 task.run(); 131 if (Dispatcher.getLastEventMillis() != lastEventMillis) 132 break; } 134 } 135 } 136 } 137 138 private Runnable updateSidebarRunnable = new Runnable () { 139 public void run() 140 { 141 Sidebar.refreshSidebarInAllFrames(); 142 } 143 }; 144 145 private IdleThreadTask updateSidebarTask = 146 new IdleThreadTask(updateSidebarRunnable, 500, true); 147 148 private Runnable parseBuffersRunnable = new Runnable () 149 { 150 public void run() 151 { 152 synchronized (Editor.getBufferList()) { 153 for (BufferIterator iter = new BufferIterator(); iter.hasNext();) { 154 Buffer buf = iter.nextBuffer(); 155 if (!buf.needsParsing()) 156 continue; 157 boolean changed = false; 158 try { 159 buf.lockRead(); 160 } 161 catch (InterruptedException e) { 162 Log.error(e); 163 return; 164 } 165 try { 166 changed = buf.getFormatter().parseBuffer(); 167 } 168 finally { 169 buf.unlockRead(); 170 } 171 if (changed) 172 buf.repaint(); 173 } 174 } 175 } 176 }; 177 178 private IdleThreadTask parseBuffersTask = 179 new IdleThreadTask(parseBuffersRunnable, 500, false); 180 181 private Runnable updateHorizontalScrollBarsRunnable = new Runnable () { 182 public void run() 183 { 184 for (EditorIterator it = new EditorIterator(); it.hasNext();) { 185 Editor ed = it.nextEditor(); 186 Buffer buf = ed.getBuffer(); 187 if (buf != null) { 188 if (buf.validateMaximumColumns()) 189 ed.updateHorizontalScrollBar(); 190 } 191 } 192 } 193 }; 194 195 private IdleThreadTask updateHorizontalScrollBarsTask = 196 new IdleThreadTask(updateHorizontalScrollBarsRunnable, 500, true); 197 198 private Runnable autosaveRunnable = new Runnable () { 199 public void run() 200 { 201 Editor editor = Editor.currentEditor(); 202 if (editor == null) 203 return; 204 Buffer buffer = editor.getBuffer(); 205 if (buffer != null) 206 buffer.autosave(); 207 } 208 }; 209 210 private IdleThreadTask autosaveTask = 211 new IdleThreadTask(autosaveRunnable, 2000, false); 212 213 private Runnable saveStateRunnable = new Runnable () { 214 private long lastRun = 0; 215 public void run() 216 { 217 Debug.assertTrue(SwingUtilities.isEventDispatchThread()); 218 if (Dispatcher.getLastEventMillis() > lastRun) { 219 Editor editor = Editor.currentEditor(); 220 if (editor != null) 221 editor.saveState(); 222 RecentFiles.getInstance().save(); 223 lastRun = System.currentTimeMillis(); 224 } 225 } 226 }; 227 228 private IdleThreadTask saveStateTask = 229 new IdleThreadTask(saveStateRunnable, 5000, true); 230 231 private IdleThreadTask gcTask = new GarbageCollectionTask(); 232 233 private IdleThreadTask tagCurrentDirectoryTask = 234 new TagCurrentDirectoryTask(); 235 236 private static IdleThreadTask followContextTask; 237 238 private void addListThreadsTask() 239 { 240 Runnable listThreadsRunnable = new Runnable () { 241 private long lastRun = 0; 242 public void run() 243 { 244 int minutes = Editor.preferences().getIntegerProperty( 245 Property.LIST_THREADS); 246 if (minutes > 0) { 247 long millis = minutes * 60000; 248 if (System.currentTimeMillis() - lastRun > millis) { 249 Debug.listThreads(); 250 lastRun = System.currentTimeMillis(); 251 } 252 } 253 } 254 }; 255 addTask(new IdleThreadTask(listThreadsRunnable, 10000, true)); 256 } 257 } 258 | Popular Tags |