blob: 9e44ef9c02f6e95c8183846e348dfc4158eee6e5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package org.openslx.bwlp.sat.maintenance;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.log4j.Logger;
public class Maintenance extends Thread {
private static final Logger LOGGER = Logger.getLogger(Maintenance.class);
private static Maintenance worker = null;
private static BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(50);
private Maintenance() {
}
private synchronized static void ensureRunning() {
if (worker == null || !worker.isAlive()) {
worker = new Maintenance();
worker.start();
}
}
public static void submit(Runnable job) throws InterruptedException {
ensureRunning();
queue.put(job);
}
public static boolean trySubmit(Runnable job) {
ensureRunning();
return queue.offer(job);
}
@Override
public void run() {
LOGGER.info("Maintenance Thread started");
try {
for (;;) {
Runnable job = queue.take();
runJob(job);
}
} catch (InterruptedException e) {
LOGGER.warn("Maintenance Thread was interrupted!", e);
if (!queue.isEmpty()) {
ensureRunning();
}
}
}
private void runJob(Runnable job) {
try {
job.run();
} catch (Throwable t) {
LOGGER.warn("Uncaught exception in job '" + job.getClass().getSimpleName() + "'", t);
}
}
}
|