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