aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-web/src/test/java/tools/refinery/language/web/tests/RestartableCachedThreadPool.java
blob: 8e5038ae9eab074044772af4a94e5e0381b71b8f (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package tools.refinery.language.web.tests;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

@SuppressWarnings("NullableProblems")
public class RestartableCachedThreadPool implements ExecutorService {
	private static final Logger LOG = LoggerFactory.getLogger(RestartableCachedThreadPool.class);

	private ExecutorService delegate;

	public RestartableCachedThreadPool() {
		delegate = createExecutorService();
	}

	public void waitForAllTasksToFinish() {
		delegate.shutdown();
		waitForTermination();
		delegate = createExecutorService();
	}

	public void waitForTermination() {
		boolean result = false;
		try {
			result = delegate.awaitTermination(1, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			LOG.warn("Interrupted while waiting for delegate executor to stop", e);
		}
		if (!result) {
			throw new IllegalStateException("Failed to shut down Xtext thread pool");
		}
	}

	protected ExecutorService createExecutorService() {
		return Executors.newCachedThreadPool();
	}

	@Override
	public boolean awaitTermination(long arg0, TimeUnit arg1) throws InterruptedException {
		return delegate.awaitTermination(arg0, arg1);
	}

	@Override
	public void execute(Runnable arg0) {
		delegate.execute(arg0);
	}

	@Override
	public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> arg0, long arg1, TimeUnit arg2)
			throws InterruptedException {
		return delegate.invokeAll(arg0, arg1, arg2);
	}

	@Override
	public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> arg0) throws InterruptedException {
		return delegate.invokeAll(arg0);
	}

	@Override
	public <T> T invokeAny(Collection<? extends Callable<T>> arg0, long arg1, TimeUnit arg2)
			throws InterruptedException, ExecutionException, TimeoutException {
		return delegate.invokeAny(arg0, arg1, arg2);
	}

	@Override
	public <T> T invokeAny(Collection<? extends Callable<T>> arg0) throws InterruptedException, ExecutionException {
		return delegate.invokeAny(arg0);
	}

	@Override
	public boolean isShutdown() {
		return delegate.isShutdown();
	}

	@Override
	public boolean isTerminated() {
		return delegate.isTerminated();
	}

	@Override
	public void shutdown() {
		delegate.shutdown();
	}

	@Override
	public List<Runnable> shutdownNow() {
		return delegate.shutdownNow();
	}

	@Override
	public <T> Future<T> submit(Callable<T> arg0) {
		return delegate.submit(arg0);
	}

	@Override
	public <T> Future<T> submit(Runnable arg0, T arg1) {
		return delegate.submit(arg0, arg1);
	}

	@Override
	public Future<?> submit(Runnable arg0) {
		return delegate.submit(arg0);
	}
}