[90] | 1 | package com.framsticks.util.dispatching; |
---|
| 2 | |
---|
[100] | 3 | import org.apache.logging.log4j.Logger; |
---|
| 4 | import org.apache.logging.log4j.LogManager; |
---|
[90] | 5 | import com.framsticks.util.dispatching.Dispatching; |
---|
[96] | 6 | import java.lang.Thread; |
---|
[102] | 7 | import java.util.concurrent.atomic.AtomicBoolean; |
---|
[90] | 8 | |
---|
| 9 | public class Monitor implements JoinableParent { |
---|
| 10 | private static final Logger log = |
---|
[100] | 11 | LogManager.getLogger(Monitor.class); |
---|
[90] | 12 | |
---|
| 13 | protected final Joinable joinable; |
---|
[96] | 14 | protected final Thread shutdownHook; |
---|
[102] | 15 | protected final AtomicBoolean dropped = new AtomicBoolean(false); |
---|
| 16 | protected final AtomicBoolean joining = new AtomicBoolean(false); |
---|
[90] | 17 | |
---|
| 18 | /** |
---|
| 19 | * @param joinable |
---|
| 20 | */ |
---|
| 21 | public Monitor(Joinable joinable) { |
---|
| 22 | this.joinable = joinable; |
---|
[96] | 23 | |
---|
| 24 | shutdownHook = new Thread(new Runnable() { |
---|
| 25 | @Override |
---|
| 26 | public void run() { |
---|
[102] | 27 | log.debug("droping and joining"); |
---|
[96] | 28 | Monitor.this.drop().join(); |
---|
[102] | 29 | log.debug("droped and joined"); |
---|
| 30 | AbstractJoinable.report(); |
---|
[96] | 31 | } |
---|
| 32 | }); |
---|
[90] | 33 | } |
---|
| 34 | |
---|
[96] | 35 | public Monitor use() { |
---|
| 36 | Runtime.getRuntime().addShutdownHook(shutdownHook); |
---|
| 37 | |
---|
[100] | 38 | log.debug("{} is using", this); |
---|
[96] | 39 | Dispatching.use(joinable, this); |
---|
| 40 | return this; |
---|
| 41 | } |
---|
| 42 | |
---|
[90] | 43 | public Monitor useFor(double seconds) { |
---|
| 44 | Dispatching.sleep(seconds); |
---|
| 45 | return this; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public Monitor waitFor() { |
---|
[100] | 49 | log.debug("{} is waiting", this); |
---|
[90] | 50 | synchronized (this) { |
---|
| 51 | while (joinable.getState().ordinal() < JoinableState.FINISHING.ordinal()) { |
---|
| 52 | Dispatching.wait(this, 100); |
---|
| 53 | } |
---|
| 54 | } |
---|
[100] | 55 | log.debug("{} ended waiting", this); |
---|
[90] | 56 | return this; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | public Monitor drop() { |
---|
[102] | 61 | if (!dropped.compareAndSet(false, true)) { |
---|
| 62 | return this; |
---|
| 63 | } |
---|
[100] | 64 | log.debug("{} is droping", this); |
---|
[90] | 65 | Dispatching.drop(joinable, this); |
---|
| 66 | return this; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public Monitor join() { |
---|
[102] | 70 | if (!joining.compareAndSet(false, true)) { |
---|
| 71 | log.debug("not joining"); |
---|
| 72 | return this; |
---|
| 73 | } |
---|
| 74 | |
---|
[100] | 75 | log.debug("{} is joining", this); |
---|
[90] | 76 | Dispatching.joinAbsolutely(joinable); |
---|
[100] | 77 | log.debug("{} is joined", this); |
---|
[96] | 78 | |
---|
| 79 | try { |
---|
| 80 | Runtime.getRuntime().removeShutdownHook(shutdownHook); |
---|
| 81 | } catch (IllegalStateException e) { |
---|
| 82 | /** In case IllegalStateException is caught, it means that JVM is in finalization stage */ |
---|
| 83 | } |
---|
[90] | 84 | return this; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | @Override |
---|
| 88 | // @SuppressWarnings("NN_NAKED_NOTIFY") |
---|
| 89 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
| 90 | synchronized (this) { |
---|
| 91 | this.notify(); |
---|
| 92 | } |
---|
[100] | 93 | log.debug("{} received notification about transition to {}", this, state); |
---|
[90] | 94 | } |
---|
| 95 | |
---|
| 96 | @Override |
---|
| 97 | public String toString() { |
---|
| 98 | return "monitor for " + joinable; |
---|
| 99 | } |
---|
| 100 | |
---|
[96] | 101 | @Override |
---|
| 102 | public Monitor getMonitor() { |
---|
| 103 | return this; |
---|
| 104 | } |
---|
| 105 | |
---|
[105] | 106 | protected ExceptionDispatcherHandler taskExceptionHandler; |
---|
[96] | 107 | |
---|
| 108 | /** |
---|
| 109 | * @return the taskExceptionHandler |
---|
| 110 | */ |
---|
[105] | 111 | public ExceptionDispatcherHandler getTaskExceptionHandler() { |
---|
[96] | 112 | return taskExceptionHandler; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * @param taskExceptionHandler the taskExceptionHandler to set |
---|
| 117 | */ |
---|
[105] | 118 | public void setTaskExceptionHandler(ExceptionDispatcherHandler taskExceptionHandler) { |
---|
[96] | 119 | this.taskExceptionHandler = taskExceptionHandler; |
---|
| 120 | } |
---|
| 121 | |
---|
[90] | 122 | } |
---|