/** * Construct a new {@code AsyncEvents} instance, that will use the given consumer to process the events. * * @param eventConsumer The {@link Consumer} used for processing the events that are sent in. */ public AsyncEvents( Consumer<T> eventConsumer, Monitor monitor ) { this.eventConsumer = eventConsumer; this.monitor = monitor; this.startupLatch = new BinaryLatch(); this.shutdownLatch = new BinaryLatch(); this.stack = END_SENTINEL; }
public boolean tryLock( int recordId ) { Integer record = recordId; BinaryLatch myLatch = new BinaryLatch(); BinaryLatch existingLatch = map.putIfAbsent( record, myLatch ); return existingLatch == null; }
public void awaitUpdateApplication() { BinaryLatch updateLatch = new BinaryLatch(); scheduler.schedule( Group.INDEX_UPDATING, updateLatch::release ); updateLatch.await(); } }
ScheduledJobHandle( TimeBasedTaskScheduler scheduler, Group group, Runnable task, long nextDeadlineNanos, long reschedulingDelayNanos ) { this.group = group; this.nextDeadlineNanos = nextDeadlineNanos; handleRelease = new BinaryLatch(); cancelListeners = new CopyOnWriteArrayList<>(); this.task = () -> { try { task.run(); // Use compareAndSet to avoid overriding any cancellation state. if ( compareAndSet( SUBMITTED, RUNNABLE ) && reschedulingDelayNanos > 0 ) { // We only reschedule if the rescheduling delay is greater than zero. // A rescheduling delay of zero means this is a delayed task. // If the rescheduling delay is greater than zero, then this is a recurring task. this.nextDeadlineNanos += reschedulingDelayNanos; scheduler.enqueueTask( this ); } } catch ( Throwable e ) { lastException = e; set( FAILED ); } }; }
public void lock( int recordId ) { Integer record = recordId; BinaryLatch myLatch = new BinaryLatch(); for (;;) { BinaryLatch existingLatch = map.putIfAbsent( record, myLatch ); if ( existingLatch == null ) { break; } else { existingLatch.await(); } } }
final AtomicReference<BinaryLatch> latchRef = new AtomicReference<>( new BinaryLatch() ); Runnable awaiter = () -> for ( int i = 0; i < 500000; i++ ) latchRef.getAndSet( new BinaryLatch() ).release(); spin( rng.nextLong( 0, 10 ) );
@Test void releaseMustUnblockAwaiters() { assertTimeout( ofSeconds( 10 ), () -> { final BinaryLatch latch = new BinaryLatch(); Runnable awaiter = latch::await; int awaiters = 24; Future<?>[] futures = new Future<?>[awaiters]; for ( int i = 0; i < awaiters; i++ ) { futures[i] = executor.submit( awaiter ); } assertThrows( TimeoutException.class, () -> futures[0].get( 10, TimeUnit.MILLISECONDS ) ); latch.release(); for ( Future<?> future : futures ) { future.get(); } } ); }
@Test void releaseThenAwaitDoesNotBlock() { assertTimeout( ofSeconds( 3 ), () -> { BinaryLatch latch = new BinaryLatch(); latch.release(); latch.await(); } ); }
@Test( timeout = 10_000 ) public void scheduledTasksThatThrowsShouldStop() throws Exception { CentralJobScheduler scheduler = new CentralJobScheduler(); scheduler.init(); BinaryLatch triggerLatch = new BinaryLatch(); RuntimeException boom = new RuntimeException( "boom" ); AtomicInteger triggerCounter = new AtomicInteger(); Runnable job = () -> { triggerCounter.incrementAndGet(); triggerLatch.release(); throw boom; }; scheduler.scheduleRecurring( Group.INDEX_POPULATION, job, 1, TimeUnit.MILLISECONDS ); triggerLatch.await(); Thread.sleep( 50 ); assertThat( triggerCounter.get(), is( 1 ) ); }
@Test void mustCombineWork() throws Exception BinaryLatch startLatch = new BinaryLatch(); BinaryLatch blockLatch = new BinaryLatch(); FutureTask<Void> blocker = new FutureTask<>( new CallableWork( new AddWork( 1 )
BinaryLatch blockLatch = new BinaryLatch(); Runnable task = () ->
BinaryLatch backgroundCheckPointStartedLatch = new BinaryLatch(); BinaryLatch forceCheckPointStartLatch = new BinaryLatch();
BinaryLatch lockerPause = new BinaryLatch(); long nodeId; try ( Transaction tx = database.beginTx() )
@Test public void longRunningTasksMustNotDelayExecutionOfOtherTasks() throws Exception { BinaryLatch latch = new BinaryLatch(); Runnable longRunning = latch::await; Runnable shortRunning = semaphore::release; scheduler.submit( Group.STORAGE_MAINTENANCE, longRunning, 100, 100 ); scheduler.submit( Group.STORAGE_MAINTENANCE, shortRunning, 100, 100 ); for ( int i = 0; i < 4; i++ ) { clock.forward( 100, TimeUnit.NANOSECONDS ); scheduler.tick(); assertSemaphoreAcquire(); } latch.release(); }
BinaryLatch limiterStartLatch = new BinaryLatch(); BinaryLatch limiterBlockLatch = new BinaryLatch(); Future<?> flusher = executor.submit( () ->
BinaryLatch startLatch = new BinaryLatch(); BinaryLatch continueLatch = new BinaryLatch();
BinaryLatch startLatch = new BinaryLatch(); BinaryLatch continueLatch = new BinaryLatch();
BinaryLatch latch = new BinaryLatch();
File c = existingFile( "c" ); BinaryLatch limiterStartLatch = new BinaryLatch(); BinaryLatch limiterBlockLatch = new BinaryLatch(); Future<?> flusher;
BinaryLatch indexUpdateBlocker = new BinaryLatch(); db.getDependencyResolver().resolveDependency( JobScheduler.class, ONLY ).schedule( Group.INDEX_UPDATING, indexUpdateBlocker::await );