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

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

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

File size: 3.2 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.log4j.Logger;
4
5import java.util.LinkedList;
6import java.util.ListIterator;
7
8import javax.annotation.OverridingMethodsMustInvokeSuper;
9
10import com.framsticks.params.annotations.ParamAnnotation;
11import com.framsticks.util.dispatching.RunAt;
12
13/**
14 * @author Piotr Sniegowski
15 */
16public class Thread<C> extends AbstractJoinable implements Dispatcher<C> {
17
18        private static final Logger log = Logger.getLogger(Thread.class.getName());
19
20        protected final java.lang.Thread thread;
21
22        private final LinkedList<Task<? extends C>> queue = new LinkedList<>();
23
24        public Thread() {
25                thread = new java.lang.Thread(new java.lang.Runnable() {
26                        @Override
27                        public void run() {
28                                Thread.this.routine();
29                        }
30                });
31        }
32
33        @OverridingMethodsMustInvokeSuper
34        protected void firstTask() {
35        }
36
37        public Thread(java.lang.Thread thread) {
38                this.thread = thread;
39        }
40
41        @Override
42        protected void joinableStart() {
43                thread.start();
44        }
45
46        @Override
47        public final boolean isActive() {
48                return thread.equals(java.lang.Thread.currentThread());
49        }
50
51        protected void routine() {
52                log.debug("starting thread " + this);
53                firstTask();
54                while (!java.lang.Thread.interrupted()) {
55                        Task<? extends C> task;
56                        synchronized (queue) {
57                                if (queue.isEmpty()) {
58                                        try {
59                                                queue.wait();
60                                        } catch (InterruptedException ignored) {
61                                                break;
62                                        }
63                                        continue;
64                                }
65                                task = queue.peekFirst();
66                                assert task != null;
67                                if (task.moment > System.currentTimeMillis()) {
68                                        try {
69                                                queue.wait(task.moment - System.currentTimeMillis());
70                                        } catch (InterruptedException ignored) {
71                                                continue;
72                                        }
73                                        continue;
74                                }
75                                queue.pollFirst();
76                        }
77                        try {
78                                task.run();
79                        } catch (Exception e) {
80                                log.error("error in thread: " + e);
81                        }
82                }
83                log.debug("finishing thread " + this);
84                finish();
85        }
86
87        protected void enqueueTask(Task<? extends C> task) {
88                synchronized (queue) {
89                        ListIterator<Task<? extends C>> i = queue.listIterator();
90                        while (i.hasNext()) {
91                                Task<? extends C> t = i.next();
92                                if (t.getMoment() > task.getMoment()) {
93                                        i.previous();
94                                        i.add(task);
95                                        task = null;
96                                        break;
97                                }
98                        }
99                        if (task != null) {
100                                queue.add(task);
101                        }
102
103                        /*
104                        Iterator<Task> j = queue.iterator();
105                        Task prev = null;
106                        while (j.hasNext()) {
107                                Task next = j.next();
108                                assert (prev == null) || prev.getMoment() <= next.getMoment();
109                                prev = next;
110                        }
111                        */
112                        queue.notify();
113                }
114        }
115
116        @Override
117        public void invokeLater(final RunAt<? extends C> runnable) {
118                if (!(runnable instanceof Task)) {
119                        enqueueTask(new Task<C>() {
120                                @Override
121                                public void run() {
122                                        runnable.run();
123                                }
124                        });
125                        return;
126                }
127                enqueueTask((Task<? extends C>) runnable);
128        }
129
130        @Override
131        protected void joinableInterrupt() {
132                thread.interrupt();
133        }
134
135        @Override
136        protected void joinableJoin() throws InterruptedException {
137                thread.join(500);
138                log.debug("joined " + this);
139        }
140
141        @ParamAnnotation
142        public void setName(String name) {
143                thread.setName(name);
144        }
145
146        @ParamAnnotation
147        public String getName() {
148                return thread.getName();
149        }
150
151        public static boolean interrupted() {
152                return java.lang.Thread.interrupted();
153        }
154
155        @Override
156        public String toString() {
157                return getName();
158        }
159
160        @Override
161        protected void joinableFinish() {
162        }
163
164}
Note: See TracBrowser for help on using the repository browser.