forked from google/guava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSameThreadScheduledExecutorService.java
188 lines (162 loc) · 6.38 KB
/
SameThreadScheduledExecutorService.java
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright (C) 2009 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.util.concurrent.testing;
import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableScheduledFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* A ScheduledExecutorService that executes all scheduled actions immediately in the calling thread.
*
* <p>See {@link TestingExecutors#sameThreadScheduledExecutor()} for a full list of constraints.
*
* @author John Sirois
* @author Zach van Schouwen
*/
@GwtIncompatible
class SameThreadScheduledExecutorService extends AbstractExecutorService
implements ListeningScheduledExecutorService {
private final ListeningExecutorService delegate = newDirectExecutorService();
@Override
public void shutdown() {
delegate.shutdown();
}
@Override
public List<Runnable> shutdownNow() {
return delegate.shutdownNow();
}
@Override
public boolean isShutdown() {
return delegate.isShutdown();
}
@Override
public boolean isTerminated() {
return delegate.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
Preconditions.checkNotNull(unit, "unit must not be null!");
return delegate.awaitTermination(timeout, unit);
}
@Override
public <T> ListenableFuture<T> submit(Callable<T> task) {
Preconditions.checkNotNull(task, "task must not be null!");
return delegate.submit(task);
}
@Override
public <T> ListenableFuture<T> submit(Runnable task, T result) {
Preconditions.checkNotNull(task, "task must not be null!");
Preconditions.checkNotNull(result, "result must not be null!");
return delegate.submit(task, result);
}
@Override
public ListenableFuture<?> submit(Runnable task) {
Preconditions.checkNotNull(task, "task must not be null!");
return delegate.submit(task);
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
Preconditions.checkNotNull(tasks, "tasks must not be null!");
return delegate.invokeAll(tasks);
}
@Override
public <T> List<Future<T>> invokeAll(
Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException {
Preconditions.checkNotNull(tasks, "tasks must not be null!");
Preconditions.checkNotNull(unit, "unit must not be null!");
return delegate.invokeAll(tasks, timeout, unit);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException {
Preconditions.checkNotNull(tasks, "tasks must not be null!");
return delegate.invokeAny(tasks);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
Preconditions.checkNotNull(tasks, "tasks must not be null!");
Preconditions.checkNotNull(unit, "unit must not be null!");
return delegate.invokeAny(tasks, timeout, unit);
}
@Override
public void execute(Runnable command) {
Preconditions.checkNotNull(command, "command must not be null!");
delegate.execute(command);
}
@Override
public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
Preconditions.checkNotNull(command, "command must not be null");
Preconditions.checkNotNull(unit, "unit must not be null!");
return schedule(Executors.callable(command), delay, unit);
}
@Override
public <V> ListenableScheduledFuture<V> schedule(
Callable<V> callable, long delay, TimeUnit unit) {
Preconditions.checkNotNull(callable, "callable must not be null!");
Preconditions.checkNotNull(unit, "unit must not be null!");
ListenableFuture<V> delegateFuture = submit(callable);
return new ImmediateScheduledFuture<>(delegateFuture);
}
private static final class ImmediateScheduledFuture<V> extends SimpleForwardingListenableFuture<V>
implements ListenableScheduledFuture<V> {
ImmediateScheduledFuture(ListenableFuture<V> future) {
super(future);
}
@Override
public V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
Preconditions.checkNotNull(unit, "unit must not be null!");
return get();
}
@Override
public long getDelay(TimeUnit unit) {
Preconditions.checkNotNull(unit, "unit must not be null!");
return 0;
}
@Override
public int compareTo(Delayed other) {
Preconditions.checkNotNull(other, "other must not be null!");
return 0;
}
}
@Override
public ListenableScheduledFuture<?> scheduleAtFixedRate(
Runnable command, long initialDelay, long period, TimeUnit unit) {
throw new UnsupportedOperationException("scheduleAtFixedRate is not supported.");
}
@Override
public ListenableScheduledFuture<?> scheduleWithFixedDelay(
Runnable command, long initialDelay, long delay, TimeUnit unit) {
throw new UnsupportedOperationException("scheduleWithFixedDelay is not supported.");
}
}