- Timestamp:
- 06/30/13 12:48:20 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/util/dispatching/Dispatching.java
r85 r88 1 1 package com.framsticks.util.dispatching; 2 3 import org.apache.log4j.Logger; 2 4 3 5 import com.framsticks.util.StateFunctor; … … 7 9 */ 8 10 public abstract class Dispatching { 11 private static final Logger log = Logger.getLogger(Dispatching.class); 9 12 10 13 public static boolean isThreadSafe() { … … 31 34 32 35 // public static boolean assertInvokeLater(Dispatcher dispatcher, RunAt runnable) { 33 // 34 // 36 // dispatcher.invokeLater(runnable); 37 // return true; 35 38 // } 36 39 … … 44 47 } 45 48 49 public static void sleep(int milliseconds) { 50 try { 51 java.lang.Thread.sleep(milliseconds, 0); 52 } catch (InterruptedException e) { 53 54 } 55 } 56 57 @SuppressWarnings("unchecked") 58 public static void dispatcherGuardedInvoke(Joinable joinable, RunAt<?> runnable) { 59 if (joinable instanceof Dispatcher) { 60 invokeLaterOrNow(Dispatcher.class.cast(joinable), runnable); 61 return; 62 } 63 runnable.run(); 64 } 65 66 public static void use(final Joinable joinable, final JoinableParent owner) { 67 log.debug("using " + joinable + " by " + owner); 68 if (joinable.use(owner)) { 69 log.debug("started " + joinable); 70 } else { 71 log.debug("start of " + joinable + " already happened"); 72 } 73 } 74 75 76 public static void drop(final Joinable joinable, final JoinableParent owner) { 77 log.debug("droping " + joinable + " by " + owner); 78 if (joinable.drop(owner)) { 79 log.debug("stoped " + joinable); 80 } else { 81 log.debug("stop of " + joinable + " deferred"); 82 } 83 } 84 85 public static void join(Joinable joinable) throws InterruptedException { 86 log.debug("joining " + joinable); 87 try { 88 joinable.join(); 89 } catch (InterruptedException e) { 90 log.debug("failed to join " + joinable); 91 throw e; 92 } 93 log.debug("joined " + joinable); 94 } 95 96 public static void childChangedState(final JoinableParent parent, final Joinable joinable, final JoinableState state) { 97 dispatcherGuardedInvoke(joinable, new RunAt<Object>() { 98 @Override 99 public void run() { 100 log.debug("joinable " + joinable + " is notifying parent " + parent + " about change to " + state); 101 parent.childChangedState(joinable, state); 102 } 103 }); 104 } 105 106 public static void wait(Object object, long millis) { 107 try { 108 object.wait(millis); 109 } catch (InterruptedException e) { 110 } 111 } 112 113 public static void joinAbsolutely(Joinable joinable) { 114 log.info("joining absolutely " + joinable); 115 while (true) { 116 try { 117 Dispatching.join(joinable); 118 return; 119 } catch (InterruptedException e) { 120 // throw new FramsticksException().msg("failed to join").arg("dispatcher", dispatcher).cause(e); 121 } 122 log.info("waiting for " + joinable); 123 sleep(500); 124 } 125 } 126 127 128 129 46 130 }
Note: See TracChangeset
for help on using the changeset viewer.