Skip to content

Commit

Permalink
Removed Scheduler's reflective lookup of delayedExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Jan 4, 2021
1 parent eabed05 commit 1006a25
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import static java.util.Objects.requireNonNull;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand All @@ -32,7 +30,6 @@

import org.checkerframework.checker.index.qual.Positive;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A scheduler that submits a task to an executor after a given delay.
Expand Down Expand Up @@ -64,15 +61,13 @@ public interface Scheduler {
}

/**
* Returns a scheduler that uses the system-wide scheduling thread if available, or else returns
* {@link #disabledScheduler()} if not present. This scheduler is provided in Java 9 or above
* by using {@link CompletableFuture} {@code delayedExecutor}.
* Returns a scheduler that uses the system-wide scheduling thread by using
* {@link CompletableFuture#delayedExecutor}.
*
* @return a scheduler that uses the system-wide scheduling thread if available, or else a
* disabled scheduler
* @return a scheduler that uses the system-wide scheduling thread
*/
static @NonNull Scheduler systemScheduler() {
return SystemScheduler.isPresent() ? SystemScheduler.INSTANCE : disabledScheduler();
return SystemScheduler.INSTANCE;
}

/**
Expand Down Expand Up @@ -101,35 +96,10 @@ public interface Scheduler {
enum SystemScheduler implements Scheduler {
INSTANCE;

static final @Nullable Method delayedExecutor = getDelayedExecutorMethod();

@Override
@SuppressWarnings("NullAway")
public Future<?> schedule(Executor executor, Runnable command, long delay, TimeUnit unit) {
requireNonNull(executor);
requireNonNull(command);
requireNonNull(unit);

try {
Executor scheduler = (Executor) delayedExecutor.invoke(
CompletableFuture.class, delay, unit, executor);
return CompletableFuture.runAsync(command, scheduler);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

static @Nullable Method getDelayedExecutorMethod() {
try {
return CompletableFuture.class.getMethod(
"delayedExecutor", long.class, TimeUnit.class, Executor.class);
} catch (NoSuchMethodException | SecurityException e) {
return null;
}
}

static boolean isPresent() {
return (delayedExecutor != null);
Executor delayedExecutor = CompletableFuture.delayedExecutor(delay, unit, executor);
return CompletableFuture.runAsync(command, delayedExecutor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.LogManager;

import org.apache.commons.lang3.SystemUtils;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.testng.annotations.DataProvider;
Expand All @@ -58,14 +57,6 @@ public final class SchedulerTest {

private final NullPointerTester npeTester = new NullPointerTester();

@Test
public void hasSystemScheduler() {
Scheduler scheduler = SystemUtils.IS_JAVA_1_8
? Scheduler.disabledScheduler()
: SystemScheduler.INSTANCE;
assertThat(Scheduler.systemScheduler(), is(scheduler));
}

@Test(dataProvider = "schedulers")
public void scheduler_null(Scheduler scheduler) {
npeTester.testAllPublicInstanceMethods(scheduler);
Expand Down

0 comments on commit 1006a25

Please sign in to comment.