- Timestamp:
- 07/16/13 23:31:35 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/util/dispatching/BufferedDispatcher.java
r101 r102 2 2 3 3 4 public class BufferedDispatcher<C> { 4 import org.apache.logging.log4j.Logger; 5 import org.apache.logging.log4j.LogManager; 6 7 import com.framsticks.util.FramsticksException; 8 import com.framsticks.util.lang.Strings; 9 // import static com.framsticks.util.dispatching.AbstractJoinable.wrap; 10 11 public class BufferedDispatcher<C> extends AbstractJoinable implements Dispatcher<C>, JoinableParent { 12 private static final Logger log = LogManager.getLogger(BufferedDispatcher.class); 5 13 6 14 protected final RunnableQueue<C> queue = new RunnableQueue<>(); … … 8 16 protected Dispatcher<C> targetDispatcher; 9 17 10 protected boolean buffer = false; 18 protected boolean buffer = true; 19 protected Dispatcher<C> parent; 20 21 /** 22 * @param parent 23 */ 24 public BufferedDispatcher(Dispatcher<C> parent) { 25 this.parent = parent; 26 } 11 27 12 28 /** … … 21 37 */ 22 38 public synchronized void setTargetDispatcher(Dispatcher<C> targetDispatcher) { 39 if (this.targetDispatcher != null) { 40 throw new FramsticksException().msg("dispatcher is already set").arg("dispatcher", this.targetDispatcher).arg("in", this); 41 } 42 if (targetDispatcher == null) { 43 throw new FramsticksException().msg("trying to set empty target dispatcher").arg("in", this); 44 } 45 log.debug("setting {} to {}", this, targetDispatcher); 23 46 this.targetDispatcher = targetDispatcher; 47 flushQueue(); 24 48 } 25 49 26 50 public synchronized boolean isActive() { 27 if (targetDispatcher == null ) {51 if (targetDispatcher == null || buffer) { 28 52 return false; 29 53 // throw new FramsticksException().msg("no dispatcher is set for tree yet").arg("tree", this); … … 40 64 } 41 65 66 protected void flushQueue() { 67 if (this.buffer || targetDispatcher == null) { 68 return; 69 } 70 log.debug("flushing {} tasks in {}", queue.size(), this); 71 while (!queue.isEmpty()) { 72 targetDispatcher.dispatch(queue.pollFirst()); 73 } 74 } 75 42 76 public synchronized void setBuffer(final boolean buffer) { 43 77 if (this.buffer == buffer) { … … 49 83 } 50 84 this.buffer = false; 51 while (!queue.isEmpty()) { 52 targetDispatcher.dispatch(queue.pollFirst()); 85 flushQueue(); 86 } 87 88 @Override 89 public String toString() { 90 return parent + " -> " + Strings.toStringNullProof(targetDispatcher, "<null>"); 91 } 92 93 public void createThreadIfNeeded() { 94 if (targetDispatcher != null) { 95 return; 53 96 } 97 this.setTargetDispatcher(new Thread<C>().setName(parent.getName())); 98 } 99 100 @Override 101 public String getName() { 102 return parent + " buffered dispatcher"; 103 } 104 105 @Override 106 protected void joinableStart() { 107 Dispatching.use(targetDispatcher, this); 108 } 109 110 @Override 111 protected void joinableInterrupt() { 112 Dispatching.drop(targetDispatcher, this); 113 114 finishJoinable(); 115 } 116 117 @Override 118 protected void joinableFinish() { 54 119 55 120 } 56 121 122 @Override 123 protected void joinableJoin() throws InterruptedException { 124 Dispatching.join(targetDispatcher); 125 } 57 126 127 @Override 128 public void childChangedState(Joinable joinable, JoinableState state) { 129 proceedToState(state); 130 } 58 131 }
Note: See TracChangeset
for help on using the changeset viewer.