source: java/main/src/main/java/com/framsticks/util/dispatching/Thread.java @ 101

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

HIGHLIGHTS:

  • improve tree side notes
  • improve GUI layout
  • add foldable list of occured events to EventControl?
  • improve automatic type conversion in proxy listeners
  • implement several Access functionalities as algorithms independent of Access type
  • introduce draft base classes for distributed experiments
  • automatically register dependant Java classes to FramsClass? registry
  • add testing prime experiment and configuration
  • simplify and improve task dispatching

CHANGELOG:
Improve task dispatching in RemoteTree?.

GUI no longer hangs on connection problems.

Make all dispatchers joinables.

Refactorize Thread dispatcher.

Remove Task and PeriodicTask?.

Use Java utilities in those situations.

Reworking tasks dispatching.

Fix bug in EventControl? listener dispatching.

Minor improvements.

Add testing configuration for ExternalProcess? in GUI.

More improvement to prime.

Support for USERREADONLY in GUI.

Add that flag to various params in Java classes.

Remove redundant register clauses from several FramsClassAnnotations?.

Automatically gather and register dependant classes.

Add configuration for prime.

Improve Simulator class.

Add prime.xml configuration.

Introduce draft Experiment and Simulator classes.

Add prime experiment tests.

Enclose typical map with listeners into SimpleUniqueList?.

Needfile works in GUI.

Improve needfile handling in Browser.

More improvement with NeedFile?.

Implementing needfile.

Update test.

Rename ChangeEvent? to TestChangeEvent?.

Automatic argument type search in RemoteTree? listeners.

MultiParamLoader? uses AccessProvider?. By default old implementation
enclosed in AccessStash? or Registry.

Minor changes.

Rename SourceInterface? to Source.

Also improve toString of File and ListSource?.

Remove unused SimpleSource? class.

Add clearing in HistoryControl?.

Show entries in table at EventControl?.

Improve EventControl?.

Add listeners registration to EventControl?.

Add foldable table to HistoryControl?.

Add control row to Procedure and Event controls.

Improve layout of controls.

Another minor change to gui layout.

Minor improvement in the SliderControl?.

Minor changes.

Move ReflectionAccess?.Backend to separate file.

It was to cluttered.

Cleanup in ReflectionAccess?.

Move setMin, setMax, setDef to AccessOperations?.

Extract loading operation into AccessOperations?.

Append Framsticks to name of UnsupportedOperationException?.

The java.lang.UnsupportedOperationException? was shadowing this class.

Rename params.Util to params.ParamsUtil?.

Several improvements.

Minor changes.

Implement revert functionality.

Improve local changes management.

Minor improvement.

Remove methods rendered superfluous after SideNoteKey? improvement.

Improve SideNoteKey?.

It is now generic type, so explicit type specification at
call site is no more needed.

Introduce SideNoteKey? interface.

Only Objects implementing that key may be used as side note keys.

Minor improvements.

Use strings instead of ValueControls? in several gui mappings.

File size: 2.6 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.logging.log4j.Logger;
4import org.apache.logging.log4j.LogManager;
5
6
7import com.framsticks.params.annotations.ParamAnnotation;
8import com.framsticks.util.dispatching.RunAt;
9
10/**
11 * @author Piotr Sniegowski
12 */
13public class Thread<C> extends AbstractJoinable implements Dispatcher<C> {
14
15        private static final Logger log = LogManager.getLogger(Thread.class);
16
17        protected final java.lang.Thread thread;
18
19        protected final Object condition = new Object();
20        private RunnableQueue<C> queue = new RunnableQueue<>();
21
22        public Thread() {
23                thread = new java.lang.Thread(new Runnable() {
24                        @Override
25                        public void run() {
26                                Thread.this.routine();
27                        }
28                });
29        }
30
31        public Thread(java.lang.Thread thread) {
32                this.thread = thread;
33        }
34
35        @Override
36        protected void joinableStart() {
37                thread.start();
38        }
39
40        @Override
41        public final boolean isActive() {
42                return thread.equals(java.lang.Thread.currentThread());
43        }
44
45        protected void routine() {
46                log.debug("starting thread {}", this);
47                assert getMonitor() != null;
48                ExceptionHandler exceptionHandler = getMonitor().getTaskExceptionHandler();
49                while (!java.lang.Thread.interrupted()) {
50                        RunAt<? extends C> runnable;
51                        synchronized (condition) {
52                                if (queue.isEmpty()) {
53                                        try {
54                                                condition.wait();
55                                        } catch (InterruptedException ignored) {
56                                                break;
57                                        }
58                                        continue;
59                                }
60                                runnable = queue.pollFirst();
61                        }
62                        if (runnable != null) {
63                                try {
64                                        runnable.run();
65                                } catch (Exception e) {
66                                        if (exceptionHandler != null) {
67                                                if (exceptionHandler.handle(this, e)) {
68                                                        continue;
69                                                }
70                                        }
71                                        log.error("error in thread: ", e);
72                                }
73                        }
74                }
75                log.debug("finishing thread {}", this);
76                finishJoinable();
77        }
78
79
80        @Override
81        public void dispatch(RunAt<? extends C> runnable) {
82                synchronized (condition) {
83                        queue.push(runnable);
84                        condition.notifyAll();
85                }
86        }
87
88        public RunnableQueue<C> switchQueue(RunnableQueue<C> queue) {
89                synchronized (condition) {
90                        RunnableQueue<C> result = this.queue;
91                        this.queue = queue;
92                        return result;
93                }
94        }
95
96        @Override
97        protected void joinableInterrupt() {
98                thread.interrupt();
99        }
100
101        @Override
102        protected void joinableJoin() throws InterruptedException {
103                thread.join(500);
104                log.debug("joined {}", this);
105        }
106
107        @ParamAnnotation
108        public void setName(String name) {
109                thread.setName(name);
110        }
111
112        @ParamAnnotation
113        public String getName() {
114                return thread.getName();
115        }
116
117        public static boolean interrupted() {
118                return java.lang.Thread.interrupted();
119        }
120
121        @Override
122        public String toString() {
123                return getName();
124        }
125
126        @Override
127        protected void joinableFinish() {
128        }
129
130}
Note: See TracBrowser for help on using the repository browser.