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