Java

Форк
0
/
SameThreadScheduledExecutorService.java 
188 строк · 6.4 Кб
1
/*
2
 * Copyright (C) 2009 The Guava Authors
3
 *
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
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
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.
15
 */
16

17
package com.google.common.util.concurrent.testing;
18

19
import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
20

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;
29
import java.util.List;
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;
38

39
/**
40
 * A ScheduledExecutorService that executes all scheduled actions immediately in the calling thread.
41
 *
42
 * <p>See {@link TestingExecutors#sameThreadScheduledExecutor()} for a full list of constraints.
43
 *
44
 * @author John Sirois
45
 * @author Zach van Schouwen
46
 */
47
@GwtIncompatible
48
class SameThreadScheduledExecutorService extends AbstractExecutorService
49
    implements ListeningScheduledExecutorService {
50

51
  private final ListeningExecutorService delegate = newDirectExecutorService();
52

53
  @Override
54
  public void shutdown() {
55
    delegate.shutdown();
56
  }
57

58
  @Override
59
  public List<Runnable> shutdownNow() {
60
    return delegate.shutdownNow();
61
  }
62

63
  @Override
64
  public boolean isShutdown() {
65
    return delegate.isShutdown();
66
  }
67

68
  @Override
69
  public boolean isTerminated() {
70
    return delegate.isTerminated();
71
  }
72

73
  @Override
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);
77
  }
78

79
  @Override
80
  public <T> ListenableFuture<T> submit(Callable<T> task) {
81
    Preconditions.checkNotNull(task, "task must not be null!");
82
    return delegate.submit(task);
83
  }
84

85
  @Override
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);
90
  }
91

92
  @Override
93
  public ListenableFuture<?> submit(Runnable task) {
94
    Preconditions.checkNotNull(task, "task must not be null!");
95
    return delegate.submit(task);
96
  }
97

98
  @Override
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);
103
  }
104

105
  @Override
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);
112
  }
113

114
  @Override
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);
119
  }
120

121
  @Override
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);
127
  }
128

129
  @Override
130
  public void execute(Runnable command) {
131
    Preconditions.checkNotNull(command, "command must not be null!");
132
    delegate.execute(command);
133
  }
134

135
  @Override
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);
140
  }
141

142
  @Override
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);
149
  }
150

151
  private static final class ImmediateScheduledFuture<V> extends SimpleForwardingListenableFuture<V>
152
      implements ListenableScheduledFuture<V> {
153
    ImmediateScheduledFuture(ListenableFuture<V> future) {
154
      super(future);
155
    }
156

157
    @Override
158
    public V get(long timeout, TimeUnit unit)
159
        throws InterruptedException, ExecutionException, TimeoutException {
160
      Preconditions.checkNotNull(unit, "unit must not be null!");
161
      return get();
162
    }
163

164
    @Override
165
    public long getDelay(TimeUnit unit) {
166
      Preconditions.checkNotNull(unit, "unit must not be null!");
167
      return 0;
168
    }
169

170
    @Override
171
    public int compareTo(Delayed other) {
172
      Preconditions.checkNotNull(other, "other must not be null!");
173
      return 0;
174
    }
175
  }
176

177
  @Override
178
  public ListenableScheduledFuture<?> scheduleAtFixedRate(
179
      Runnable command, long initialDelay, long period, TimeUnit unit) {
180
    throw new UnsupportedOperationException("scheduleAtFixedRate is not supported.");
181
  }
182

183
  @Override
184
  public ListenableScheduledFuture<?> scheduleWithFixedDelay(
185
      Runnable command, long initialDelay, long delay, TimeUnit unit) {
186
    throw new UnsupportedOperationException("scheduleWithFixedDelay is not supported.");
187
  }
188
}
189

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.