Java Code Examples for java.util.concurrent.Phaser#register()

The following examples show how to use java.util.concurrent.Phaser#register() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTaskExecutor.java    From presto with Apache License 2.0 6 votes vote down vote up
public TestingJob(TestingTicker ticker, Phaser globalPhaser, Phaser beginQuantaPhaser, Phaser endQuantaPhaser, int requiredPhases, int quantaTimeMillis)
{
    this.ticker = ticker;
    this.globalPhaser = globalPhaser;
    this.beginQuantaPhaser = beginQuantaPhaser;
    this.endQuantaPhaser = endQuantaPhaser;
    this.requiredPhases = requiredPhases;
    this.quantaTimeMillis = quantaTimeMillis;

    beginQuantaPhaser.register();
    endQuantaPhaser.register();

    if (globalPhaser.getRegisteredParties() == 0) {
        globalPhaser.register();
    }
}
 
Example 2
Source File: CriticalPathView.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Signal handler for analysis started, we need to rebuilt the entry list with
 * updated statistics values for the current graph worker of the critical path
 * module.
 *
 * @param signal
 *            The signal
 */
@TmfSignalHandler
public void analysisStarted(TmfStartAnalysisSignal signal) {
    IAnalysisModule analysis = signal.getAnalysisModule();
    if (analysis instanceof CriticalPathModule) {
        CriticalPathModule criticalPath = (CriticalPathModule) analysis;
        /*
         * We need to wait for CriticalPathDataProviderFactory to have
         * received this signal. Create a new phaser and register, and wait
         * for the end synch signal to arrive and advance in a new thread.
         */
        fPhaser = new Phaser();
        fPhaser.register();
        new Thread() {
            @Override
            public void run() {
                fPhaser.awaitAdvance(0);
                Collection<ITmfTrace> traces = TmfTraceManager.getTraceSetWithExperiment(getTrace());
                if (traces.contains(criticalPath.getTrace())) {
                    rebuild();
                }
            }
        }.start();
    }
}
 
Example 3
Source File: PhaseOverflow.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 4
Source File: PhaseOverflow.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 5
Source File: PhaseOverflow.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 6
Source File: PhaseOverflow.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 7
Source File: PhaseOverflow.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 8
Source File: PhaseOverflow.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 9
Source File: TieredArriveLoops.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
Runnable runner(final Phaser p) {
    return new CheckedRunnable() { public void realRun() {
        int prevPhase = p.register();
        while (!p.isTerminated()) {
            int phase = p.awaitAdvance(p.arrive());
            if (phase < 0)
                return;
            equal(phase, (prevPhase + 1) & Integer.MAX_VALUE);
            int ph = p.getPhase();
            check(ph < 0 || ph == phase);
            prevPhase = phase;
        }
    }};
}
 
Example 10
Source File: PhaseOverflow.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 11
Source File: PhaseOverflow.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 12
Source File: SchedulersMetricsTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Parameters(method = "metricsSchedulers")
@Test(timeout = 10_000)
public void shouldReportExecutionTimes(Supplier<Scheduler> schedulerSupplier, String type) {
    Scheduler scheduler = afterTest.autoDispose(schedulerSupplier.get());
    final int taskCount = 3;

	Phaser phaser = new Phaser(1);
	for (int i = 1; i <= taskCount; i++) {
		phaser.register();
		int delay = i * 200; //bumped delay from 20ms to make actual scheduling times more precise
		scheduler.schedule(() -> {
			try {
				Thread.sleep(delay);
				phaser.arriveAndDeregister();
			}
			catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		});
	}
	phaser.arriveAndAwaitAdvance();

	Collection<Timer> timers = simpleMeterRegistry
			.find("executor")
			.tag(TAG_SCHEDULER_ID, scheduler.toString())
			.timers();

	// Use Awaitility because "count" is reported "eventually"
	await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
		assertThat(timers.stream()
		                 .reduce(0d, (time, timer) -> time + timer.totalTime(TimeUnit.MILLISECONDS), Double::sum))
				.as("total durations")
				.isEqualTo(600 + 400 + 200, offset(50d));
		assertThat(timers.stream().mapToLong(Timer::count).sum())
				.as("count")
				.isEqualTo(taskCount);
	});
}
 
Example 13
Source File: PhaseOverflow.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 14
Source File: TestTaskExecutor.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskHandle()
{
    TestingTicker ticker = new TestingTicker();
    TaskExecutor taskExecutor = new TaskExecutor(4, 8, 3, 4, ticker);
    taskExecutor.start();

    try {
        TaskId taskId = new TaskId("test", 0, 0);
        TaskHandle taskHandle = taskExecutor.addTask(taskId, () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());

        Phaser beginPhase = new Phaser();
        beginPhase.register();
        Phaser verificationComplete = new Phaser();
        verificationComplete.register();

        TestingJob driver1 = new TestingJob(ticker, new Phaser(), beginPhase, verificationComplete, 10, 0);
        TestingJob driver2 = new TestingJob(ticker, new Phaser(), beginPhase, verificationComplete, 10, 0);

        // force enqueue a split
        taskExecutor.enqueueSplits(taskHandle, true, ImmutableList.of(driver1));
        assertEquals(taskHandle.getRunningLeafSplits(), 0);

        // normal enqueue a split
        taskExecutor.enqueueSplits(taskHandle, false, ImmutableList.of(driver2));
        assertEquals(taskHandle.getRunningLeafSplits(), 1);

        // let the split continue to run
        beginPhase.arriveAndDeregister();
        verificationComplete.arriveAndDeregister();
    }
    finally {
        taskExecutor.stop();
    }
}
 
Example 15
Source File: TestTaskExecutor.java    From presto with Apache License 2.0 4 votes vote down vote up
@Test(invocationCount = 100)
public void testTasksComplete()
        throws Exception
{
    TestingTicker ticker = new TestingTicker();
    TaskExecutor taskExecutor = new TaskExecutor(4, 8, 3, 4, ticker);
    taskExecutor.start();
    ticker.increment(20, MILLISECONDS);

    try {
        TaskId taskId = new TaskId("test", 0, 0);
        TaskHandle taskHandle = taskExecutor.addTask(taskId, () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());

        Phaser beginPhase = new Phaser();
        beginPhase.register();
        Phaser verificationComplete = new Phaser();
        verificationComplete.register();

        // add two jobs
        TestingJob driver1 = new TestingJob(ticker, new Phaser(), beginPhase, verificationComplete, 10, 0);
        ListenableFuture<?> future1 = getOnlyElement(taskExecutor.enqueueSplits(taskHandle, true, ImmutableList.of(driver1)));
        TestingJob driver2 = new TestingJob(ticker, new Phaser(), beginPhase, verificationComplete, 10, 0);
        ListenableFuture<?> future2 = getOnlyElement(taskExecutor.enqueueSplits(taskHandle, true, ImmutableList.of(driver2)));
        assertEquals(driver1.getCompletedPhases(), 0);
        assertEquals(driver2.getCompletedPhases(), 0);

        // verify worker have arrived but haven't processed yet
        beginPhase.arriveAndAwaitAdvance();
        assertEquals(driver1.getCompletedPhases(), 0);
        assertEquals(driver2.getCompletedPhases(), 0);
        ticker.increment(60, SECONDS);
        assertEquals(taskExecutor.getRunAwaySplitCount(), 0);
        ticker.increment(600, SECONDS);
        assertEquals(taskExecutor.getRunAwaySplitCount(), 2);
        verificationComplete.arriveAndAwaitAdvance();

        // advance one phase and verify
        beginPhase.arriveAndAwaitAdvance();
        assertEquals(driver1.getCompletedPhases(), 1);
        assertEquals(driver2.getCompletedPhases(), 1);

        verificationComplete.arriveAndAwaitAdvance();

        // add one more job
        TestingJob driver3 = new TestingJob(ticker, new Phaser(), beginPhase, verificationComplete, 10, 0);
        ListenableFuture<?> future3 = getOnlyElement(taskExecutor.enqueueSplits(taskHandle, false, ImmutableList.of(driver3)));

        // advance one phase and verify
        beginPhase.arriveAndAwaitAdvance();
        assertEquals(driver1.getCompletedPhases(), 2);
        assertEquals(driver2.getCompletedPhases(), 2);
        assertEquals(driver3.getCompletedPhases(), 0);
        verificationComplete.arriveAndAwaitAdvance();

        // advance to the end of the first two task and verify
        beginPhase.arriveAndAwaitAdvance();
        for (int i = 0; i < 7; i++) {
            verificationComplete.arriveAndAwaitAdvance();
            beginPhase.arriveAndAwaitAdvance();
            assertEquals(beginPhase.getPhase(), verificationComplete.getPhase() + 1);
        }
        assertEquals(driver1.getCompletedPhases(), 10);
        assertEquals(driver2.getCompletedPhases(), 10);
        assertEquals(driver3.getCompletedPhases(), 8);
        future1.get(1, SECONDS);
        future2.get(1, SECONDS);
        verificationComplete.arriveAndAwaitAdvance();

        // advance two more times and verify
        beginPhase.arriveAndAwaitAdvance();
        verificationComplete.arriveAndAwaitAdvance();
        beginPhase.arriveAndAwaitAdvance();
        assertEquals(driver1.getCompletedPhases(), 10);
        assertEquals(driver2.getCompletedPhases(), 10);
        assertEquals(driver3.getCompletedPhases(), 10);
        future3.get(1, SECONDS);
        verificationComplete.arriveAndAwaitAdvance();

        assertEquals(driver1.getFirstPhase(), 0);
        assertEquals(driver2.getFirstPhase(), 0);
        assertEquals(driver3.getFirstPhase(), 2);

        assertEquals(driver1.getLastPhase(), 10);
        assertEquals(driver2.getLastPhase(), 10);
        assertEquals(driver3.getLastPhase(), 12);

        // no splits remaining
        ticker.increment(610, SECONDS);
        assertEquals(taskExecutor.getRunAwaySplitCount(), 0);
    }
    finally {
        taskExecutor.stop();
    }
}
 
Example 16
Source File: DeoptimizeFramesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public TestCaseImpl(Phaser phaser) {
    this.phaser = phaser;
    phaser.register();
}
 
Example 17
Source File: DataTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
public void serverIncrementPriority(boolean online)
    throws DatabaseException, TimeoutException, InterruptedException {
  DatabaseConfig cfg = IntegrationTestHelpers.newTestConfig();
  DatabaseReference ref = IntegrationTestHelpers.rootWithConfig(cfg);
  List<Object> foundPriorities = new ArrayList<>();
  // Note: all numeric values will be long or double, so they must be cast before being inserted.
  List<Object> expectedPriorities = new ArrayList<>();

  // Going offline ensures that local events get queued up before server events
  if (!online) {
    IntegrationTestHelpers.goOffline(cfg);
  }

  // Phaser is the closest built-in to a bidrectional latch. We could use a semaphore with a fixed
  // number of permits, but the test would be fragile since the permit count isn't closely related
  // to the test cases.
  final Phaser latch = new Phaser(0);

  ValueEventListener listener =
      ref.addValueEventListener(
          new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
              foundPriorities.add(snapshot.getPriority());
              latch.arrive();
            }

            @Override
            public void onCancelled(DatabaseError error) {}
          });

  try {
    // null + increment
    latch.register();
    ref.setValue(0, ServerValue.increment(1));
    // numeric priorities are always doubles
    expectedPriorities.add(1.0);

    // long + double
    latch.register();
    ref.setValue(0, ServerValue.increment(1.5));
    expectedPriorities.add(2.5);

    // Checking types first makes failures much more obvious
    latch.awaitAdvanceInterruptibly(0, IntegrationTestValues.getTimeout(), MILLISECONDS);
    assertEquals(expectedPriorities, foundPriorities);
  } finally {
    ref.removeEventListener(listener);
    if (!online) {
      IntegrationTestHelpers.goOnline(cfg);
    }
  }
}
 
Example 18
Source File: DataTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
public void serverIncrementOverwritesExistingData(boolean online)
    throws DatabaseException, TimeoutException, InterruptedException {
  DatabaseConfig cfg = IntegrationTestHelpers.newTestConfig();
  DatabaseReference ref = IntegrationTestHelpers.rootWithConfig(cfg);
  List<Object> foundValues = new ArrayList<>();
  // Note: all numeric values will be long, so they must be cast before being inserted.
  List<Object> expectedValues = new ArrayList<>();

  // Going offline ensures that local events get queued up before server events
  if (!online) {
    IntegrationTestHelpers.goOffline(cfg);
  }

  // Phaser is the closest built-in to a bidrectional latch. We could use a semaphore with a fixed
  // number of permits, but the test would be fragile since the permit count isn't closely related
  // to the test cases.
  final Phaser latch = new Phaser(0);

  ValueEventListener listener =
      ref.addValueEventListener(
          new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
              foundValues.add(snapshot.getValue());
              latch.arrive();
            }

            @Override
            public void onCancelled(DatabaseError error) {}
          });

  try {
    // null + incr
    latch.register();
    ref.setValue(ServerValue.increment(1));
    expectedValues.add((long) 1);

    // number + incr
    latch.bulkRegister(2);
    ref.setValue(5);
    ref.setValue(ServerValue.increment(1));
    expectedValues.add((long) 5);
    expectedValues.add((long) 6);

    // string + incr
    latch.bulkRegister(2);
    ref.setValue("hello");
    ref.setValue(ServerValue.increment(1));
    expectedValues.add("hello");
    expectedValues.add((long) 1);

    // object + incr
    latch.bulkRegister(2);
    Map<String, Object> obj = new MapBuilder().put("hello", "world").build();
    ref.setValue(obj);
    ref.setValue(ServerValue.increment(1));
    expectedValues.add(obj);
    expectedValues.add((long) 1);

    latch.awaitAdvanceInterruptibly(0, IntegrationTestValues.getTimeout(), MILLISECONDS);
    assertEquals(expectedValues, foundValues);
  } finally {
    ref.removeEventListener(listener);
    if (!online) {
      IntegrationTestHelpers.goOnline(cfg);
    }
  }
}
 
Example 19
Source File: LongRunningAction.java    From tutorials with MIT License 4 votes vote down vote up
LongRunningAction(String threadName, Phaser ph) {
    this.threadName = threadName;
    this.ph = ph;
    ph.register();
}
 
Example 20
Source File: SuperConsoleEventBusListenerTest.java    From buck with Apache License 2.0 4 votes vote down vote up
ProgressEstimatorSynchronization(BuckEventBus eventBus) {
  phaser = new Phaser();
  phaser.register();

  eventBus.register(this);
}