- Timestamp:
- 07/02/13 16:20:07 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/util/dispatching/Dispatching.java
r88 r90 15 15 } 16 16 17 public static <C> void invokeLaterOrNow(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) {17 public static <C> void dispatchIfNotActive(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) { 18 18 if (dispatcher.isActive()) { 19 19 runnable.run(); 20 20 return; 21 21 } 22 dispatcher. invokeLater(runnable);22 dispatcher.dispatch(runnable); 23 23 } 24 24 25 25 //TODO RunAt StateFunctor 26 26 public static <C> void dispatchOk(Dispatcher<C> dispatcher, final StateFunctor stateFunctor) { 27 dispatcher. invokeLater(new RunAt<C>() {27 dispatcher.dispatch(new RunAt<C>() { 28 28 @Override 29 29 public void run() { … … 39 39 40 40 public static <P, C> void invokeDispatch(Dispatcher<P> dispatcher, final Dispatcher<C> finalDispatcher, final RunAt<C> runnable) { 41 dispatcher. invokeLater(new RunAt<P>() {41 dispatcher.dispatch(new RunAt<P>() { 42 42 @Override 43 43 public void run() { 44 finalDispatcher. invokeLater(runnable);44 finalDispatcher.dispatch(runnable); 45 45 } 46 46 }); 47 47 } 48 48 49 public static void sleep( int milliseconds) {49 public static void sleep(double seconds) { 50 50 try { 51 java.lang.Thread.sleep( milliseconds, 0);51 java.lang.Thread.sleep((long) (seconds * 1000)); 52 52 } catch (InterruptedException e) { 53 53 … … 58 58 public static void dispatcherGuardedInvoke(Joinable joinable, RunAt<?> runnable) { 59 59 if (joinable instanceof Dispatcher) { 60 invokeLaterOrNow(Dispatcher.class.cast(joinable), runnable);60 dispatchIfNotActive(Dispatcher.class.cast(joinable), runnable); 61 61 return; 62 62 } … … 72 72 } 73 73 } 74 75 74 76 75 public static void drop(final Joinable joinable, final JoinableParent owner) { … … 106 105 public static void wait(Object object, long millis) { 107 106 try { 108 object.wait(millis); 107 synchronized (object) { 108 object.wait(millis); 109 } 109 110 } catch (InterruptedException e) { 110 111 } … … 112 113 113 114 public static void joinAbsolutely(Joinable joinable) { 114 log. info("joining absolutely " + joinable);115 log.debug("joining absolutely " + joinable); 115 116 while (true) { 116 117 try { … … 120 121 // throw new FramsticksException().msg("failed to join").arg("dispatcher", dispatcher).cause(e); 121 122 } 122 log. info("waiting for " + joinable);123 sleep(500);123 log.debug("waiting for " + joinable); 124 wait(joinable, 500); 124 125 } 126 } 127 128 public interface Query<T> { 129 T get(); 130 } 131 132 public static class QueryRunner<T, C> extends RunAt<C> { 133 protected final Query<T> query; 134 T result; 135 boolean ready = false; 136 137 /** 138 * @param query 139 */ 140 public QueryRunner(Query<T> query) { 141 this.query = query; 142 } 143 144 @Override 145 public void run() { 146 result = query.get(); 147 synchronized (this) { 148 ready = true; 149 this.notifyAll(); 150 } 151 } 152 153 public T get() { 154 synchronized (this) { 155 while (!ready) { 156 try { 157 this.wait(100); 158 } catch (InterruptedException e) { 159 } 160 } 161 } 162 return result; 163 } 164 } 165 166 public static <T, C> T get(Dispatcher<C> dispatcher, Query<T> query) { 167 QueryRunner<T, C> runner = new QueryRunner<T, C>(query); 168 dispatcher.dispatch(runner); 169 return runner.get(); 125 170 } 126 171 127 172 128 173 129 130 174 }
Note: See TracChangeset
for help on using the changeset viewer.