public final boolean fastEnter() { return wip.get() == 0 && wip.compareAndSet(0, 1); }
private boolean isSkip() { int skip = connectSkip.get(); // Growth of skipping times if (skip >= 10) { // If the number of skipping times increases by more than 10, take the random number if (connectRandom == 0) { connectRandom = ThreadLocalRandom.current().nextInt(10); } skip = 10 + connectRandom; } if (connectSkipped.getAndIncrement() < skip) { // Check the number of skipping times return true; } connectSkip.incrementAndGet(); connectSkipped.set(0); connectRandom = 0; return false; }
public void beforeOpen() { int invoking = openingCount.incrementAndGet(); for (;;) { int max = openingMax.get(); if (invoking > max) { if (openingMax.compareAndSet(max, invoking)) { break; } } else { break; } } openCount.incrementAndGet(); lastOpenTime = System.currentTimeMillis(); }
@Test public void testUnsubscribeOnNestedTakeAndSyncInfiniteStream() throws InterruptedException { final AtomicInteger subscribeCounter = new AtomicInteger(); final AtomicInteger sentEventCounter = new AtomicInteger(); doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(SYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); Thread.sleep(500); assertEquals(39, sentEventCounter.get()); }
@Test public void verifyDirtiesContextBehavior() throws Exception { runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase.class, 1); assertContextCacheStatistics("after class-level @DirtiesContext with clean test method and default class mode", 0, cacheHits.get(), cacheMisses.incrementAndGet()); }
private static int count(Iterable<Node> nodes) { assertNotNull(nodes); AtomicInteger count = new AtomicInteger(); nodes.forEach(n -> count.incrementAndGet()); return count.get(); }
@Override public void run() { counter.incrementAndGet(); System.out.println("counter: " + counter.get()); inner.schedule(this); }
@Test public void testUnsubscribeOnNestedTakeAndSyncInfiniteStream() throws InterruptedException { final AtomicInteger subscribeCounter = new AtomicInteger(); final AtomicInteger sentEventCounter = new AtomicInteger(); doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(SYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); Thread.sleep(500); assertEquals(39, sentEventCounter.get()); }
public void testForEachPair_parallel() { Stream<String> streamA = IntStream.range(0, 100000).mapToObj(String::valueOf).parallel(); Stream<Integer> streamB = IntStream.range(0, 100000).mapToObj(i -> i).parallel(); AtomicInteger count = new AtomicInteger(0); Streams.forEachPair( streamA, streamB, (a, b) -> { count.incrementAndGet(); Truth.assertThat(a.equals(String.valueOf(b))).isTrue(); }); Truth.assertThat(count.get()).isEqualTo(100000); // of course, this test doesn't prove that anything actually happened in parallel... }
public void beforeExecute() { int invoking = runningCount.incrementAndGet(); for (;;) { int max = concurrentMax.get(); if (invoking > max) { if (concurrentMax.compareAndSet(max, invoking)) { break; } } else { break; } } count.incrementAndGet(); lastSampleTime = System.currentTimeMillis(); }
@Override public void run() { counter.incrementAndGet(); System.out.println("counter: " + counter.get()); inner.schedule(this); }
private boolean isSkip() { int skip = connectSkip.get(); // Growth of skipping times if (skip >= 10) { // If the number of skipping times increases by more than 10, take the random number if (connectRandom == 0) { connectRandom = ThreadLocalRandom.current().nextInt(10); } skip = 10 + connectRandom; } if (connectSkipped.getAndIncrement() < skip) { // Check the number of skipping times return true; } connectSkip.incrementAndGet(); connectSkipped.set(0); connectRandom = 0; return false; }
public final boolean fastEnter() { return wip.get() == 0 && wip.compareAndSet(0, 1); }
@Test public void testUnsubscribeOnNestedTakeAndAsyncInfiniteStream() throws InterruptedException { final AtomicInteger subscribeCounter = new AtomicInteger(); final AtomicInteger sentEventCounter = new AtomicInteger(); doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(ASYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); Thread.sleep(500); assertEquals(39, sentEventCounter.get()); }
@Override public void run() { final AtomicInteger nexts = new AtomicInteger(); try { Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(NUM_RETRIES)); TestObserver<String> to = new TestObserver<String>(); origin.retry() .observeOn(Schedulers.computation()).subscribe(to); to.awaitTerminalEvent(2500, TimeUnit.MILLISECONDS); List<String> onNextEvents = new ArrayList<String>(to.values()); if (onNextEvents.size() != NUM_RETRIES + 2) { for (Throwable t : to.errors()) { onNextEvents.add(t.toString()); } for (long err = to.completions(); err != 0; err--) { onNextEvents.add("onComplete"); } data.put(j, onNextEvents); } } catch (Throwable t) { timeouts.incrementAndGet(); System.out.println(j + " | " + cdl.getCount() + " !!! " + nexts.get()); } cdl.countDown(); } });
@Override public void onComplete() { threadsRunning.incrementAndGet(); System.out.println(">>> Busyobserver received onComplete"); onComplete = true; int concurrentThreads = threadsRunning.get(); int maxThreads = maxConcurrentThreads.get(); if (concurrentThreads > maxThreads) { maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads); } threadsRunning.decrementAndGet(); }
@Override public void onRemoval(RemovalNotification<Integer, AtomicInteger> notification) { if (notification.wasEvicted()) { evictionCount.incrementAndGet(); totalSum.addAndGet(notification.getValue().get()); } } };
static boolean reserveSpace(AtomicInteger availableSharedCapacity, int space) { assert space >= 0; for (;;) { int available = availableSharedCapacity.get(); if (available < space) { return false; } if (availableSharedCapacity.compareAndSet(available, available - space)) { return true; } } } }
@Test public void testUnsubscribeOnNestedTakeAndAsyncInfiniteStream() throws InterruptedException { final AtomicInteger subscribeCounter = new AtomicInteger(); final AtomicInteger sentEventCounter = new AtomicInteger(); doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(ASYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); Thread.sleep(500); assertEquals(39, sentEventCounter.get()); }
@Test public void testRevocationAlreadyRequested() { AtomicInteger counter = new AtomicInteger(); OperatorContext operatorContext = TestingOperatorContext.create(scheduledExecutor); LocalMemoryContext revocableMemoryContext = operatorContext.localRevocableMemoryContext(); revocableMemoryContext.setBytes(1000); // when memory revocation is already requested setting a listener should immediately execute it operatorContext.requestMemoryRevoking(); operatorContext.setMemoryRevocationRequestListener(() -> counter.incrementAndGet()); assertTrue(operatorContext.isMemoryRevokingRequested()); assertEquals(counter.get(), 1); }