1 8 9 package foxtrot; 10 11 import javax.swing.SwingUtilities ; 12 13 import foxtrot.pumps.ConditionalEventPump; 14 import foxtrot.pumps.QueueEventPump; 15 import foxtrot.pumps.SunJDK14ConditionalEventPump; 16 17 21 abstract class AbstractSyncWorker extends AbstractWorker 22 { 23 private EventPump eventPump; 24 25 AbstractSyncWorker() 26 { 27 } 28 29 36 EventPump eventPump() 37 { 38 if (eventPump == null) eventPump(createDefaultEventPump()); 39 return eventPump; 40 } 41 42 49 void eventPump(EventPump eventPump) 50 { 51 if (eventPump == null) throw new IllegalArgumentException ("EventPump cannot be null"); 52 this.eventPump = eventPump; 53 if (debug) System.out.println("[AbstractSyncWorker] Initialized EventPump: " + eventPump); 54 } 55 56 59 EventPump createDefaultEventPump() 60 { 61 if (JREVersion.isJRE14()) 62 { 63 return new SunJDK14ConditionalEventPump(); 65 } 66 else if (JREVersion.isJRE13()) 67 { 68 return new ConditionalEventPump(); 69 } 70 else if (JREVersion.isJRE12()) 71 { 72 return new QueueEventPump(); 73 } 74 else 75 { 76 throw new Error ("The current JRE is not supported"); 77 } 78 } 79 80 85 Object post(Task task, WorkerThread workerThread, EventPump eventPump) throws Exception 86 { 87 boolean isEventThread = SwingUtilities.isEventDispatchThread(); 88 if (!isEventThread && !workerThread.isWorkerThread()) 89 { 90 throw new IllegalStateException ("Method post() can be called only from the AWT Event Dispatch Thread or from a worker thread"); 91 } 92 93 if (isEventThread) 94 { 95 workerThread.postTask(task); 96 97 eventPump.pumpEvents(task); 99 } 100 else 101 { 102 workerThread.runTask(task); 104 } 105 106 try 107 { 108 return task.getResultOrThrow(); 109 } 110 finally 111 { 112 task.reset(); 113 } 114 } 115 116 120 Object post(Job job, WorkerThread workerThread, EventPump eventPump) 121 { 122 try 123 { 124 return post((Task)job, workerThread, eventPump); 125 } 126 catch (RuntimeException x) 127 { 128 throw x; 129 } 130 catch (Exception x) 131 { 132 if (debug) 134 { 135 System.err.println("[Worker] PANIC: checked exception thrown by a Job !"); 136 x.printStackTrace(); 137 } 138 139 throw new RuntimeException (x.toString()); 142 } 143 catch (Error x) 144 { 145 throw x; 146 } 147 } 148 } 149 | Popular Tags |