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 | import com.framsticks.util.dispatching.RunAt; |
---|
8 | |
---|
9 | /** |
---|
10 | * @author Piotr Sniegowski |
---|
11 | */ |
---|
12 | public class Thread<C> implements Dispatcher<C> { |
---|
13 | |
---|
14 | private static final Logger log = Logger.getLogger(Thread.class.getName()); |
---|
15 | |
---|
16 | protected final java.lang.Thread thread; |
---|
17 | |
---|
18 | private final LinkedList<Task<? extends C>> queue = new LinkedList<>(); |
---|
19 | |
---|
20 | public Thread() { |
---|
21 | thread = new java.lang.Thread(new java.lang.Runnable() { |
---|
22 | @Override |
---|
23 | public void run() { |
---|
24 | Thread.this.routine(); |
---|
25 | } |
---|
26 | }); |
---|
27 | } |
---|
28 | |
---|
29 | public Thread(String s) { |
---|
30 | this(); |
---|
31 | thread.setName(s); |
---|
32 | // thread.start(); |
---|
33 | } |
---|
34 | |
---|
35 | public Thread(String s, java.lang.Thread thread) { |
---|
36 | this.thread = thread; |
---|
37 | thread.setName(s); |
---|
38 | } |
---|
39 | |
---|
40 | public Thread<C> start() { |
---|
41 | thread.start(); |
---|
42 | return this; |
---|
43 | } |
---|
44 | |
---|
45 | @Override |
---|
46 | public final boolean isActive() { |
---|
47 | return thread.equals(java.lang.Thread.currentThread()); |
---|
48 | } |
---|
49 | |
---|
50 | protected void routine() { |
---|
51 | while (!java.lang.Thread.interrupted()) { |
---|
52 | Task<? extends C> task; |
---|
53 | synchronized (queue) { |
---|
54 | if (queue.isEmpty()) { |
---|
55 | try { |
---|
56 | queue.wait(); |
---|
57 | } catch (InterruptedException ignored) { |
---|
58 | break; |
---|
59 | } |
---|
60 | continue; |
---|
61 | } |
---|
62 | task = queue.peekFirst(); |
---|
63 | assert task != null; |
---|
64 | if (task.moment > System.currentTimeMillis()) { |
---|
65 | try { |
---|
66 | queue.wait(task.moment - System.currentTimeMillis()); |
---|
67 | } catch (InterruptedException ignored) { |
---|
68 | continue; |
---|
69 | } |
---|
70 | continue; |
---|
71 | } |
---|
72 | queue.pollFirst(); |
---|
73 | } |
---|
74 | try { |
---|
75 | task.run(); |
---|
76 | } catch (Exception e) { |
---|
77 | log.error("error in thread: " + e); |
---|
78 | e.printStackTrace(); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | protected void enqueueTask(Task<? extends C> task) { |
---|
84 | synchronized (queue) { |
---|
85 | ListIterator<Task<? extends C>> i = queue.listIterator(); |
---|
86 | while (i.hasNext()) { |
---|
87 | Task<? extends C> t = i.next(); |
---|
88 | if (t.getMoment() > task.getMoment()) { |
---|
89 | i.previous(); |
---|
90 | i.add(task); |
---|
91 | task = null; |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | if (task != null) { |
---|
96 | queue.add(task); |
---|
97 | } |
---|
98 | |
---|
99 | /* |
---|
100 | Iterator<Task> j = queue.iterator(); |
---|
101 | Task prev = null; |
---|
102 | while (j.hasNext()) { |
---|
103 | Task next = j.next(); |
---|
104 | assert (prev == null) || prev.getMoment() <= next.getMoment(); |
---|
105 | prev = next; |
---|
106 | } |
---|
107 | */ |
---|
108 | queue.notify(); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | @Override |
---|
113 | public void invokeLater(final RunAt<? extends C> runnable) { |
---|
114 | if (!(runnable instanceof Task)) { |
---|
115 | enqueueTask(new Task<C>() { |
---|
116 | @Override |
---|
117 | public void run() { |
---|
118 | runnable.run(); |
---|
119 | } |
---|
120 | }); |
---|
121 | return; |
---|
122 | } |
---|
123 | enqueueTask((Task<? extends C>) runnable); |
---|
124 | } |
---|
125 | |
---|
126 | public void interrupt() { |
---|
127 | thread.interrupt(); |
---|
128 | } |
---|
129 | |
---|
130 | public void join() throws InterruptedException { |
---|
131 | thread.join(); |
---|
132 | } |
---|
133 | |
---|
134 | public void setName(String name) { |
---|
135 | thread.setName(name); |
---|
136 | |
---|
137 | } |
---|
138 | |
---|
139 | public static boolean interrupted() { |
---|
140 | return java.lang.Thread.interrupted(); |
---|
141 | } |
---|
142 | } |
---|