use of java.util.concurrent.ScheduledExecutorService in project hive by apache.
the class ReplChangeManager method scheduleCMClearer.
// Schedule CMClearer thread. Will be invoked by metastore
public static void scheduleCMClearer(HiveConf hiveConf) {
if (HiveConf.getBoolVar(hiveConf, HiveConf.ConfVars.REPLCMENABLED)) {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new BasicThreadFactory.Builder().namingPattern("cmclearer-%d").daemon(true).build());
executor.scheduleAtFixedRate(new CMClearer(hiveConf.get(HiveConf.ConfVars.REPLCMDIR.varname), hiveConf.getTimeVar(ConfVars.REPLCMRETIAN, TimeUnit.SECONDS), hiveConf), 0, hiveConf.getTimeVar(ConfVars.REPLCMINTERVAL, TimeUnit.SECONDS), TimeUnit.SECONDS);
}
}
use of java.util.concurrent.ScheduledExecutorService in project elasticsearch by elastic.
the class CacheTests method testDependentKeyDeadlock.
public void testDependentKeyDeadlock() throws BrokenBarrierException, InterruptedException {
class Key {
private final int key;
Key(int key) {
this.key = key;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Key key1 = (Key) o;
return key == key1.key;
}
@Override
public int hashCode() {
return key % 2;
}
}
int numberOfThreads = randomIntBetween(2, 32);
final Cache<Key, Integer> cache = CacheBuilder.<Key, Integer>builder().build();
CopyOnWriteArrayList<ExecutionException> failures = new CopyOnWriteArrayList<>();
CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
CountDownLatch deadlockLatch = new CountDownLatch(numberOfThreads);
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < numberOfThreads; i++) {
Thread thread = new Thread(() -> {
try {
try {
barrier.await();
} catch (BrokenBarrierException | InterruptedException e) {
throw new AssertionError(e);
}
Random random = new Random(random().nextLong());
for (int j = 0; j < numberOfEntries; j++) {
Key key = new Key(random.nextInt(numberOfEntries));
try {
cache.computeIfAbsent(key, k -> {
if (k.key == 0) {
return 0;
} else {
Integer value = cache.get(new Key(k.key / 2));
return value != null ? value : 0;
}
});
} catch (ExecutionException e) {
failures.add(e);
break;
}
}
} finally {
// successfully avoided deadlock, release the main thread
deadlockLatch.countDown();
}
});
threads.add(thread);
thread.start();
}
AtomicBoolean deadlock = new AtomicBoolean();
assert !deadlock.get();
// start a watchdog service
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {
Set<Long> ids = threads.stream().map(t -> t.getId()).collect(Collectors.toSet());
ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
long[] deadlockedThreads = mxBean.findDeadlockedThreads();
if (!deadlock.get() && deadlockedThreads != null) {
for (long deadlockedThread : deadlockedThreads) {
// ensure that we detected deadlock on our threads
if (ids.contains(deadlockedThread)) {
deadlock.set(true);
// release the main test thread to fail the test
for (int i = 0; i < numberOfThreads; i++) {
deadlockLatch.countDown();
}
break;
}
}
}
}, 1, 1, TimeUnit.SECONDS);
// everything is setup, release the hounds
barrier.await();
// wait for either deadlock to be detected or the threads to terminate
deadlockLatch.await();
// shutdown the watchdog service
scheduler.shutdown();
assertThat(failures, is(empty()));
assertFalse("deadlock", deadlock.get());
}
use of java.util.concurrent.ScheduledExecutorService in project mongo-java-driver by mongodb.
the class Mongo method createCursorCleaningService.
private ExecutorService createCursorCleaningService() {
ScheduledExecutorService newTimer = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory("CleanCursors"));
newTimer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
cleanCursors();
}
}, 1, 1, SECONDS);
return newTimer;
}
use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.
the class Java8Example method main.
@SuppressWarnings("unused")
public static void main(String... args) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
RetryPolicy retryPolicy = new RetryPolicy();
// Create a retryable functional interface
Function<String, String> bar = value -> Failsafe.with(retryPolicy).get(() -> value + "bar");
// Create a retryable runnable Stream
Failsafe.with(retryPolicy).run(() -> Stream.of("foo").map(value -> value + "bar").forEach(System.out::println));
// Create a retryable callable Stream
Failsafe.with(retryPolicy).get(() -> Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).collect(Collectors.toList()));
// Create a individual retryable Stream operation
Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).forEach(System.out::println);
// Create a retryable CompletableFuture
Failsafe.with(retryPolicy).with(executor).future(() -> CompletableFuture.supplyAsync(() -> "foo").thenApplyAsync(value -> value + "bar").thenAccept(System.out::println));
// Create an individual retryable CompletableFuture stages
CompletableFuture.supplyAsync(() -> Failsafe.with(retryPolicy).get(() -> "foo")).thenApplyAsync(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).thenAccept(System.out::println);
}
use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.
the class AsyncFailsafeTest method shouldHandleInitialSchedulingFailure.
/**
* Asserts that Failsafe handles an initial scheduling failure.
*/
public void shouldHandleInitialSchedulingFailure() throws Throwable {
// Given
ScheduledExecutorService executor = Executors.newScheduledThreadPool(0);
executor.shutdownNow();
Waiter waiter = new Waiter();
// When
@SuppressWarnings("unchecked") FailsafeFuture<Void> future = Failsafe.with(new RetryPolicy().retryWhen(null).retryOn(Exception.class)).with(executor).run(() -> waiter.fail("Should not execute callable since executor has been shutdown"));
assertThrows(() -> future.get(), ExecutionException.class, RejectedExecutionException.class);
}
Aggregations