下面列出了怎么用java.util.concurrent.AbstractExecutorService的API类实例代码及写法,或者点击链接到github查看源代码。
private static ExecutorService phasedExecutor(Phaser phaser) {
return new AbstractExecutorService() {
final ExecutorService delegate = Executors.newFixedThreadPool(32);
@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 { return delegate.awaitTermination(timeout, unit); }
@Override public void execute(Runnable command) {
phaser.register();
delegate.execute(() -> {
command.run();
phaser.arriveAndDeregister();
});
}
};
}
@Override
protected ExecutorService getDefaultExecutorService() {
if (isMultiThreaded()) {
return super.getDefaultExecutorService();
}
return new AbstractExecutorService() {
boolean terminated;
public void shutdown() {
terminated = true;
}
public List<Runnable> shutdownNow() {
shutdown();
return null;
}
public boolean isShutdown() {
return terminated;
}
public boolean isTerminated() {
return terminated;
}
public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException {
shutdown();
return terminated;
}
public void execute(Runnable runnable) {
runnable.run();
}
};
}
private static Executor wrapExecutor(Executor executor) {
if (executor instanceof TaskExecutorService && executor instanceof AbstractExecutorService) {
return executor;
} else {
return new GenericExecutorWrapper(executor);
}
}
public static ExecutorService buildSyncynchronesExecutorService() {
return new AbstractExecutorService() {
@Override
public void shutdown() {
}
@Override
public List<Runnable> shutdownNow() {
return null;
}
@Override
public boolean isShutdown() {
return false;
}
@Override
public boolean isTerminated() {
return false;
}
@Override
public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException {
return false;
}
@Override
public void execute(Runnable runnable) {
runnable.run();
}
};
}
public static ExecutorService inThreadExecutor() {
return new AbstractExecutorService() {
final AtomicBoolean shutDown = new AtomicBoolean(false);
@Override public void shutdown() { shutDown.set(true); }
@Override public List<Runnable> shutdownNow() { shutDown.set(true); return Collections.emptyList(); }
@Override public boolean isShutdown() { return shutDown.get(); }
@Override public boolean isTerminated() { return shutDown.get(); }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) { return true; }
@Override public void execute(Runnable command) { command.run(); }
};
}
@ExpectWarning("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
public void test(AbstractExecutorService service, Callable<T> callable, Runnable runnable, T value) {
service.submit(callable);
service.submit(runnable);
service.submit(runnable, value);
}
@Override
protected ExecutorService getDefaultExecutorService() {
if (isMultiThreaded()) {
return super.getDefaultExecutorService();
}
return new AbstractExecutorService() {
boolean terminated;
public void shutdown() {
terminated = true;
}
public List<Runnable> shutdownNow() {
shutdown();
return null;
}
public boolean isShutdown() {
return terminated;
}
public boolean isTerminated() {
return terminated;
}
public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException {
shutdown();
return terminated;
}
public void execute(Runnable runnable) {
runnable.run();
}
};
}
/**
* Create an {@link ExecutorService} which runs jobs in main thread.
*/
public static ExecutorService newSynchronousExecutorService() {
return new AbstractExecutorService() {
@Override
public void execute(Runnable command) {
command.run();
}
@Override
public void shutdown() {
throw new UnsupportedOperationException();
}
@Override
public List<Runnable> shutdownNow() {
throw new UnsupportedOperationException();
}
@Override
public boolean isShutdown() {
throw new UnsupportedOperationException();
}
@Override
public boolean isTerminated() {
throw new UnsupportedOperationException();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}
};
}
private static ExecutorService wrapExecutor(final Executor executor)
{
return new AbstractExecutorService()
{
private volatile boolean isShutdown = false;
private volatile boolean isTerminated = false;
@Override
public void shutdown()
{
isShutdown = true;
}
@Override
public List<Runnable> shutdownNow()
{
return Lists.newArrayList();
}
@Override
public boolean isShutdown()
{
return isShutdown;
}
@Override
public boolean isTerminated()
{
return isTerminated;
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
{
throw new UnsupportedOperationException();
}
@Override
public void execute(Runnable command)
{
try
{
executor.execute(command);
}
finally
{
isShutdown = true;
isTerminated = true;
}
}
};
}
public ExecutorServiceThreadPool(final AbstractExecutorService pool) {
this.pool = pool;
}
@Override
public AbstractExecutorService executor() {
return pool;
}
Future falsePositive(AbstractExecutorService e, Runnable r) {
return e.submit(r, null);
}
@Test
public void failingExecutorServiceIsNotTerminated() {
AtomicInteger count = new AtomicInteger();
final IllegalStateException boom = new IllegalStateException("boom");
ExecutorService service = new AbstractExecutorService() {
boolean shutdown;
@Override
public void shutdown() {
shutdown = true;
}
@NotNull
@Override
public List<Runnable> shutdownNow() {
return Collections.emptyList();
}
@Override
public boolean isShutdown() {
return shutdown;
}
@Override
public boolean isTerminated() {
return shutdown;
}
@Override
public boolean awaitTermination(long timeout, @NotNull TimeUnit unit)
throws InterruptedException {
return false;
}
@Override
public void execute(@NotNull Runnable command) {
if (count.incrementAndGet() % 2 == 0)
throw boom;
}
};
ExecutorScheduler scheduler = new ExecutorScheduler(service, false);
assertThatCode(() -> scheduler.schedule(() -> {}))
.as("initial-no rejection")
.doesNotThrowAnyException();
assertThatExceptionOfType(RejectedExecutionException.class)
.as("second-transient rejection")
.isThrownBy(() -> scheduler.schedule(() -> {}))
.withCause(boom);
assertThatCode(() -> scheduler.schedule(() -> {}))
.as("third-no rejection")
.doesNotThrowAnyException();
assertThat(count.get()).isEqualTo(3);
}
@Test
public void failingAndShutDownExecutorServiceIsTerminated() {
final IllegalStateException boom = new IllegalStateException("boom");
ExecutorService service = new AbstractExecutorService() {
boolean shutdown;
@Override
public void shutdown() {
shutdown = true;
}
@NotNull
@Override
public List<Runnable> shutdownNow() {
return Collections.emptyList();
}
@Override
public boolean isShutdown() {
return shutdown;
}
@Override
public boolean isTerminated() {
return shutdown;
}
@Override
public boolean awaitTermination(long timeout, @NotNull TimeUnit unit)
throws InterruptedException {
return false;
}
@Override
public void execute(@NotNull Runnable command) {
if (shutdown) throw boom;
shutdown = true;
}
};
ExecutorScheduler scheduler = new ExecutorScheduler(service, false);
assertThatCode(() -> scheduler.schedule(() -> {}))
.as("initial-no rejection")
.doesNotThrowAnyException();
assertThatExceptionOfType(RejectedExecutionException.class)
.as("second-transient rejection")
.isThrownBy(() -> scheduler.schedule(() -> {}))
.withCause(boom);
assertThatExceptionOfType(RejectedExecutionException.class)
.as("third scheduler terminated rejection")
.isThrownBy(() -> scheduler.schedule(() -> {}))
.isSameAs(Exceptions.failWithRejected())
.withNoCause();
}
private static ExecutorService wrapExecutor(final Executor executor)
{
return new AbstractExecutorService()
{
private volatile boolean isShutdown = false;
private volatile boolean isTerminated = false;
@Override
public void shutdown()
{
isShutdown = true;
}
@Override
public List<Runnable> shutdownNow()
{
return Lists.newArrayList();
}
@Override
public boolean isShutdown()
{
return isShutdown;
}
@Override
public boolean isTerminated()
{
return isTerminated;
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
{
throw new UnsupportedOperationException();
}
@Override
public void execute(Runnable command)
{
try
{
executor.execute(command);
}
finally
{
isShutdown = true;
isTerminated = true;
}
}
};
}
AbstractExecutorService executor();