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

Last change on this file since 102 was 102, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

for Joinables running

CHANGELOG:
Add WorkPackageLogic? and classes representing prime experiment state.

Add classes for PrimeExperiment? state.

Extract single netload routine in Simulator.

Working netload with dummy content in PrimeExperiment?.

More development with NetLoadSaveLogic? and PrimeExperiment?.

Improvement around prime.

Improve BufferedDispatcher?.isActive logic.

Add prime-all.xml configuration.

Manual connecting to existing simulators from GUI.

Guard in SimulatorConnector? against expdef mismatch.

Guard against empty target dispatcher in BufferedDispatcher?.

Make BufferedDispatcher? a Dispatcher (and Joinable).

Minor improvements.

Done StackedJoinable?, improve Experiment.

Develop StackedJoinable?.

Add StackedJoinable? utility joinables controller.

Add dependency on apache-commons-lang.

Add ready ListChange? on Simulators.

Improve hints in ListChange?.

Several improvements.

Found bug with dispatching in Experiment.

Minor improvements.

Fix bug with early finishing Server.

Many changes in Dispatching.

Fix bug with connection.

Do not obfuscate log with socket related exceptions.

Add SocketClosedException?.

Add SimulatorConnector?.

Work out conception of experiment composing of logics building blocks.

Rename SinkInterface? to Sink.

Move saving of Accesses into AccessOperations?.

Some improvements to Experiment.

Improve joinables.

Fix issue with joinables closing.

Add direct and managed consoles to popup menu.

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 ExceptionHandler taskExceptionHandler;
107
108        /**
109         * @return the taskExceptionHandler
110         */
111        public ExceptionHandler getTaskExceptionHandler() {
112                return taskExceptionHandler;
113        }
114
115        /**
116         * @param taskExceptionHandler the taskExceptionHandler to set
117         */
118        public void setTaskExceptionHandler(ExceptionHandler taskExceptionHandler) {
119                this.taskExceptionHandler = taskExceptionHandler;
120        }
121
122}
Note: See TracBrowser for help on using the repository browser.