1 | package dk.deepthought.sidious.util; |
2 | |
3 | import java.util.LinkedList; |
4 | import java.util.List; |
5 | |
6 | import net.jcip.annotations.ThreadSafe; |
7 | |
8 | import org.apache.commons.logging.Log; |
9 | import org.apache.commons.logging.LogFactory; |
10 | |
11 | /** |
12 | * Abstract representation of a queue which uses a thread to dequeue and process |
13 | * elements. |
14 | * <p> |
15 | * Implements a work queue; which allows for asynchronous processing of enqueued |
16 | * items. |
17 | * |
18 | * @author Deepthought |
19 | * |
20 | */ |
21 | @ThreadSafe |
22 | public abstract class SidiousQueue<T> { |
23 | /** |
24 | * Logger for this class |
25 | */ |
26 | private static final Log logger = LogFactory.getLog(SidiousQueue.class); |
27 | |
28 | /** |
29 | * Private thread implementation. |
30 | */ |
31 | private class InternalThread implements Runnable { |
32 | public void run() { |
33 | if (logger.isDebugEnabled()) { |
34 | logger.debug("run() - start"); |
35 | } |
36 | |
37 | while (true) { |
38 | T item = null; |
39 | synchronized (queue) { |
40 | while (queue.isEmpty() && !interrupted) { |
41 | try { |
42 | if (logger.isDebugEnabled()) { |
43 | logger.debug("run() - waiting - start"); |
44 | } |
45 | queue.wait(); |
46 | if (logger.isDebugEnabled()) { |
47 | logger.debug("run() - waiting - end"); |
48 | } |
49 | } catch (InterruptedException e) { |
50 | logger.error("run()", e); |
51 | return; |
52 | } |
53 | } |
54 | if (interrupted) { |
55 | if (logger.isDebugEnabled()) { |
56 | logger.debug("run() - interrupted"); |
57 | } |
58 | |
59 | return; |
60 | } |
61 | item = queue.remove(0); |
62 | if (logger.isDebugEnabled()) { |
63 | logger.debug("run() - " + item + " dequeued"); |
64 | } |
65 | } |
66 | process(item); |
67 | } |
68 | } |
69 | } |
70 | |
71 | /** |
72 | * Boolean to indicate whether the thread has been interrupted. |
73 | */ |
74 | private boolean interrupted = false; |
75 | |
76 | /** |
77 | * Internal queue. |
78 | */ |
79 | private final List<T> queue = new LinkedList<T>(); |
80 | |
81 | /** |
82 | * Constructor that starts a new thread. |
83 | */ |
84 | protected SidiousQueue(String name) { |
85 | startThread(name); |
86 | } |
87 | |
88 | /** |
89 | * Enqueues an item. |
90 | * |
91 | * @param item |
92 | * the item to be enqueued |
93 | */ |
94 | public final void enqueue(T item) { |
95 | if (logger.isDebugEnabled()) { |
96 | logger.debug("enqueue(item=" + item + ") - start"); |
97 | } |
98 | synchronized (queue) { |
99 | queue.add(item); |
100 | queue.notify(); |
101 | } |
102 | |
103 | if (logger.isDebugEnabled()) { |
104 | logger.debug("enqueue(item=" + item + ") - end"); |
105 | } |
106 | } |
107 | |
108 | /** |
109 | * Interrupts the thread. |
110 | */ |
111 | public final void interrupt() { |
112 | if (logger.isDebugEnabled()) { |
113 | logger.debug("interrupt() - start"); |
114 | } |
115 | synchronized (queue) { |
116 | interrupted = true; |
117 | queue.notify(); |
118 | } |
119 | if (logger.isDebugEnabled()) { |
120 | logger.debug("interrupt() - end"); |
121 | } |
122 | } |
123 | |
124 | /** |
125 | * The processing of an item. |
126 | * |
127 | * @param item |
128 | * the item to be processed. |
129 | */ |
130 | protected abstract void process(T item); |
131 | |
132 | /** |
133 | * Method to start the thread. |
134 | * |
135 | * @param name |
136 | * the name of the thread |
137 | */ |
138 | private void startThread(String name) { |
139 | Thread thread = new Thread(new InternalThread(), name); |
140 | thread.setDaemon(false); |
141 | thread.start(); |
142 | if (logger.isDebugEnabled()) { |
143 | logger.debug("startThread() - Thread started"); |
144 | } |
145 | } |
146 | |
147 | } |