2
* Copyright (C) 2009 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.util.concurrent.testing;
19
import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
21
import com.google.common.annotations.GwtIncompatible;
22
import com.google.common.base.Preconditions;
23
import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
24
import com.google.common.util.concurrent.ListenableFuture;
25
import com.google.common.util.concurrent.ListenableScheduledFuture;
26
import com.google.common.util.concurrent.ListeningExecutorService;
27
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
28
import java.util.Collection;
30
import java.util.concurrent.AbstractExecutorService;
31
import java.util.concurrent.Callable;
32
import java.util.concurrent.Delayed;
33
import java.util.concurrent.ExecutionException;
34
import java.util.concurrent.Executors;
35
import java.util.concurrent.Future;
36
import java.util.concurrent.TimeUnit;
37
import java.util.concurrent.TimeoutException;
40
* A ScheduledExecutorService that executes all scheduled actions immediately in the calling thread.
42
* <p>See {@link TestingExecutors#sameThreadScheduledExecutor()} for a full list of constraints.
45
* @author Zach van Schouwen
48
class SameThreadScheduledExecutorService extends AbstractExecutorService
49
implements ListeningScheduledExecutorService {
51
private final ListeningExecutorService delegate = newDirectExecutorService();
54
public void shutdown() {
59
public List<Runnable> shutdownNow() {
60
return delegate.shutdownNow();
64
public boolean isShutdown() {
65
return delegate.isShutdown();
69
public boolean isTerminated() {
70
return delegate.isTerminated();
74
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
75
Preconditions.checkNotNull(unit, "unit must not be null!");
76
return delegate.awaitTermination(timeout, unit);
80
public <T> ListenableFuture<T> submit(Callable<T> task) {
81
Preconditions.checkNotNull(task, "task must not be null!");
82
return delegate.submit(task);
86
public <T> ListenableFuture<T> submit(Runnable task, T result) {
87
Preconditions.checkNotNull(task, "task must not be null!");
88
Preconditions.checkNotNull(result, "result must not be null!");
89
return delegate.submit(task, result);
93
public ListenableFuture<?> submit(Runnable task) {
94
Preconditions.checkNotNull(task, "task must not be null!");
95
return delegate.submit(task);
99
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
100
throws InterruptedException {
101
Preconditions.checkNotNull(tasks, "tasks must not be null!");
102
return delegate.invokeAll(tasks);
106
public <T> List<Future<T>> invokeAll(
107
Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
108
throws InterruptedException {
109
Preconditions.checkNotNull(tasks, "tasks must not be null!");
110
Preconditions.checkNotNull(unit, "unit must not be null!");
111
return delegate.invokeAll(tasks, timeout, unit);
115
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
116
throws InterruptedException, ExecutionException {
117
Preconditions.checkNotNull(tasks, "tasks must not be null!");
118
return delegate.invokeAny(tasks);
122
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
123
throws InterruptedException, ExecutionException, TimeoutException {
124
Preconditions.checkNotNull(tasks, "tasks must not be null!");
125
Preconditions.checkNotNull(unit, "unit must not be null!");
126
return delegate.invokeAny(tasks, timeout, unit);
130
public void execute(Runnable command) {
131
Preconditions.checkNotNull(command, "command must not be null!");
132
delegate.execute(command);
136
public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
137
Preconditions.checkNotNull(command, "command must not be null");
138
Preconditions.checkNotNull(unit, "unit must not be null!");
139
return schedule(Executors.callable(command), delay, unit);
143
public <V> ListenableScheduledFuture<V> schedule(
144
Callable<V> callable, long delay, TimeUnit unit) {
145
Preconditions.checkNotNull(callable, "callable must not be null!");
146
Preconditions.checkNotNull(unit, "unit must not be null!");
147
ListenableFuture<V> delegateFuture = submit(callable);
148
return new ImmediateScheduledFuture<>(delegateFuture);
151
private static final class ImmediateScheduledFuture<V> extends SimpleForwardingListenableFuture<V>
152
implements ListenableScheduledFuture<V> {
153
ImmediateScheduledFuture(ListenableFuture<V> future) {
158
public V get(long timeout, TimeUnit unit)
159
throws InterruptedException, ExecutionException, TimeoutException {
160
Preconditions.checkNotNull(unit, "unit must not be null!");
165
public long getDelay(TimeUnit unit) {
166
Preconditions.checkNotNull(unit, "unit must not be null!");
171
public int compareTo(Delayed other) {
172
Preconditions.checkNotNull(other, "other must not be null!");
178
public ListenableScheduledFuture<?> scheduleAtFixedRate(
179
Runnable command, long initialDelay, long period, TimeUnit unit) {
180
throw new UnsupportedOperationException("scheduleAtFixedRate is not supported.");
184
public ListenableScheduledFuture<?> scheduleWithFixedDelay(
185
Runnable command, long initialDelay, long delay, TimeUnit unit) {
186
throw new UnsupportedOperationException("scheduleWithFixedDelay is not supported.");