1 19 package org.netbeans.test.editor.app.util; 20 21 import java.util.ArrayList ; 22 import javax.swing.SwingUtilities ; 23 28 public class Scheduler extends Thread { 29 30 private static Object synchronizeTo = new Object (); 31 private static boolean allowedToCreate = true; 32 private ArrayList runnables; 33 private ArrayList run; 34 private static Scheduler scheduler = null; 35 private boolean shouldFinish = false; 36 37 private static boolean superSafe = false; 38 private static boolean superSafeChanged = false; 39 40 public static boolean getSuperSafe() { 41 return superSafe; 42 } 43 44 public static void setSuperSafe(boolean newState) { 45 if (superSafeChanged) 46 return; 47 superSafe = newState; 48 superSafeChanged = true; 49 } 50 51 public static synchronized Scheduler getDefault() { 52 if (scheduler == null ) { 53 new Scheduler(); 54 scheduler.start(); 55 } 56 return scheduler; 57 } 58 59 public static void finishScheduler() { 60 if (scheduler == null) { 61 66 } else { 67 getDefault().finish(); 68 } 69 } 70 71 72 protected Scheduler() { 73 runnables = new ArrayList (); 74 run = new ArrayList (); 75 scheduler = this; 76 } 77 78 public void addTask(Runnable what) { 79 synchronized (runnables) { 80 runnables.add(what); 81 } 82 } 83 84 public void finish() { 85 shouldFinish = true; 86 } 88 89 public void run() { 90 boolean finish = false; 91 92 while (!finish) { 93 while (!shouldFinish || (runnables.size() > 0)) { 94 if (runnables.size() > 0) { 95 Runnable toRun = null; 96 synchronized (runnables) { 97 toRun = (Runnable )runnables.get(0); 98 runnables.remove(0); 99 } 100 if (superSafe) { 101 SwingUtilities.invokeLater(toRun); 102 } else { 103 try { 104 SwingUtilities.invokeAndWait(toRun); 105 } catch (java.lang.InterruptedException e) { 106 System.err.println("Unexpected interrupt of invokeAndWait(): " + e); 107 e.printStackTrace(System.err); 108 } catch (java.lang.reflect.InvocationTargetException e) { 109 System.err.println("Unexpected exception in invokeAndWait: " + e); 110 e.printStackTrace(System.err); 111 } 112 } 113 try { 114 sleep(10); 115 } catch (InterruptedException e) { 116 System.err.println("Interrupted."); 117 } 118 } else { 119 try { 121 sleep(200); 122 } catch (Exception ex) { 123 } 124 } 125 } 126 synchronized (synchronizeTo) { 127 if (runnables.size() > 0) { 128 finish = false; 129 } else { 130 finish = true; 131 scheduler = null; 132 } 133 } 134 } 135 } 136 } 137 | Popular Tags |