source: java/main/src/main/java/com/framsticks/util/dispatching/Monitor.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.logging.log4j.Logger;
4import org.apache.logging.log4j.LogManager;
5import com.framsticks.util.dispatching.Dispatching;
6import java.lang.Thread;
7import java.util.concurrent.atomic.AtomicBoolean;
8
9public class Monitor implements JoinableParent {
10        private static final Logger log =
11                LogManager.getLogger(Monitor.class);
12
13        protected final Joinable joinable;
14        protected final Thread shutdownHook;
15        protected final AtomicBoolean dropped = new AtomicBoolean(false);
16        protected final AtomicBoolean joining = new AtomicBoolean(false);
17
18        /**
19         * @param joinable
20         */
21        public Monitor(Joinable joinable) {
22                this.joinable = joinable;
23
24                shutdownHook = new Thread(new Runnable() {
25                        @Override
26                        public void run() {
27                                log.debug("droping and joining");
28                                Monitor.this.drop().join();
29                                log.debug("droped and joined");
30                                AbstractJoinable.report();
31                        }
32                });
33        }
34
35        public Monitor use() {
36                Runtime.getRuntime().addShutdownHook(shutdownHook);
37
38                log.debug("{} is using", this);
39                Dispatching.use(joinable, this);
40                return this;
41        }
42
43        public Monitor useFor(double seconds) {
44                Dispatching.sleep(seconds);
45                return this;
46        }
47
48        public Monitor waitFor() {
49                log.debug("{} is waiting", this);
50                synchronized (this) {
51                        while (joinable.getState().ordinal() < JoinableState.FINISHING.ordinal()) {
52                                Dispatching.wait(this, 100);
53                        }
54                }
55                log.debug("{} ended waiting", this);
56                return this;
57        }
58
59
60        public Monitor drop() {
61                if (!dropped.compareAndSet(false, true)) {
62                        return this;
63                }
64                log.debug("{} is droping", this);
65                Dispatching.drop(joinable, this);
66                return this;
67        }
68
69        public Monitor join() {
70                if (!joining.compareAndSet(false, true)) {
71                        log.debug("not joining");
72                        return this;
73                }
74
75                log.debug("{} is joining", this);
76                Dispatching.joinAbsolutely(joinable);
77                log.debug("{} is joined", this);
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                }
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                }
93                log.debug("{} received notification about transition to {}", this, state);
94        }
95
96        @Override
97        public String toString() {
98                return "monitor for " + joinable;
99        }
100
101        @Override
102        public Monitor getMonitor() {
103                return this;
104        }
105
106        protected ExceptionDispatcherHandler taskExceptionHandler;
107
108        /**
109         * @return the taskExceptionHandler
110         */
111        public ExceptionDispatcherHandler getTaskExceptionHandler() {
112                return taskExceptionHandler;
113        }
114
115        /**
116         * @param taskExceptionHandler the taskExceptionHandler to set
117         */
118        public void setTaskExceptionHandler(ExceptionDispatcherHandler taskExceptionHandler) {
119                this.taskExceptionHandler = taskExceptionHandler;
120        }
121
122}
Note: See TracBrowser for help on using the repository browser.