All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.facebook.concurrency.UnstoppableExecutorService Maven / Gradle / Ivy

/*
 * Copyright (C) 2012 Facebook, Inc.
 *
 * 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.facebook.concurrency;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * the idea here is that we want to shield an ExecutorService from any call
 * that might shut it down, while preserving the semantics that any user
 * of this object can still shut it down, await termination, etc 
 *  from its perspective
 * 
 * Think of it as a localized view of another executor
 */
public class UnstoppableExecutorService implements ExecutorService {
  private final UnstoppableExecutorServiceCore executorCore;
  private final ExecutorService executor;

  public UnstoppableExecutorService(ExecutorService executor) {
    this.executor = executor;
    executorCore = new UnstoppableExecutorServiceCore();
  }

  @Override
  public  Future submit(Callable task) {
    return executor.submit(executorCore.registerTask(task));
  }

  @Override
  public  Future submit(Runnable task, T result) {
    TrackedRunnable trackedTask = executorCore.registerTask(task);

    return executorCore.trackFuture(executor.submit(trackedTask, result), trackedTask);
  }

  @Override
  public Future submit(Runnable task) {
    TrackedRunnable trackedTask = executorCore.registerTask(task);
    
    return executorCore.trackFuture(executor.submit(trackedTask), trackedTask);
  }

  @Override
  public  List> invokeAll(Collection> tasks)
    throws InterruptedException {
    
    List> trackedTaskList = executorCore.registerCallableList(tasks);

    return executorCore.trackFutureList(executor.invokeAll(trackedTaskList), trackedTaskList);
  }

  @Override
  public  List> invokeAll(
    Collection> tasks, long timeout, TimeUnit unit
  ) throws InterruptedException {
    
    List> trackedTaskList = 
      executorCore.registerCallableList(tasks);

    return executorCore.trackFutureList(
      executor.invokeAll(trackedTaskList, timeout, unit), trackedTaskList
    );
  }

  @Override
  public  T invokeAny(Collection> tasks)
    throws InterruptedException, ExecutionException {
    
    List> trackedTaskList = executorCore.registerCallableList(tasks);
    

    return executor.invokeAny(trackedTaskList);
  }

  @Override
  public  T invokeAny(Collection> tasks, long timeout, TimeUnit unit)
    throws InterruptedException, ExecutionException, TimeoutException {
    
    List> trackedTaskList = executorCore.registerCallableList(tasks);

    return executor.invokeAny(trackedTaskList, timeout, unit);
  }

  @Override
  public void execute(Runnable command) {
    executor.execute(executorCore.registerTask(command));
  }

  // shutdown/termination functions delegate to the UnstoppableExecutorCore
  @Override
  public void shutdown() {
    executorCore.shutdown();
  }

  @Override
  public List shutdownNow() {
    return executorCore.shutdownNow();
  }

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

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

  @Override
  public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
    return executorCore.awaitTermination(timeout, unit);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy