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 , 11 years ago | |
---|---|
File size: 3.2 KB |
Line | |
---|---|
1 | package com.framsticks.util.dispatching; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | |
5 | import java.util.LinkedList; |
6 | import java.util.ListIterator; |
7 | |
8 | import javax.annotation.OverridingMethodsMustInvokeSuper; |
9 | |
10 | import com.framsticks.params.annotations.ParamAnnotation; |
11 | import com.framsticks.util.dispatching.RunAt; |
12 | |
13 | /** |
14 | * @author Piotr Sniegowski |
15 | */ |
16 | public 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.