Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 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 java.util.List;
Factory methods for ExecutorService for testing.

Author(s):
Chris Nokleberg
Since:
14.0
 
 public final class TestingExecutors {
   private TestingExecutors() {}

  
Returns a ScheduledExecutorService that never executes anything.

The shutdownNow method of the returned executor always returns an empty list despite the fact that everything is still technically awaiting execution. The getDelay method of any java.util.concurrent.ScheduledFuture returned by the executor will always return the max long value instead of the time until the user-specified delay.

 
     return new NoOpScheduledExecutorService();
   }
 
   private static final class NoOpScheduledExecutorService
 
     private volatile boolean shutdown;
 
     @Override public void shutdown() {
        = true;
     }
 
     @Override public List<RunnableshutdownNow() {
       shutdown();
       return ImmutableList.of();
     }
 
     @Override public boolean isShutdown() {
       return ;
     }
 
     @Override public boolean isTerminated() {
       return ;
     }
 
     @Override public boolean awaitTermination(long timeoutTimeUnit unit) {
       return true;
     }
 
     @Override public void execute(Runnable runnable) {}
 
     @Override public <V> ScheduledFuture<V> schedule(
         Callable<V> callablelong delayTimeUnit unit) {
       return NeverScheduledFuture.create();
     }
 
     @Override public ScheduledFuture<?> schedule(Runnable commandlong delayTimeUnit unit) {
       return NeverScheduledFuture.create();
     }
 
         Runnable commandlong initialDelaylong periodTimeUnit unit) {
       return NeverScheduledFuture.create();
     }
 
         Runnable commandlong initialDelaylong delayTimeUnit unit) {
       return NeverScheduledFuture.create();
     }
    private static class NeverScheduledFuture<V>
        extends AbstractFuture<V> implements ScheduledFuture<V> {
      static <V> NeverScheduledFuture<V> create() {
        return new NeverScheduledFuture<V>();
      }
      @Override public long getDelay(TimeUnit unit) {
        return .;
      }
      @Override public int compareTo(Delayed other) {
        return Longs.compare(getDelay(.), other.getDelay(.));
      }
    }
  }
New to GrepCode? Check out our FAQ X