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

The following examples show how to use java.util.concurrent.Phaser#arriveAndAwaitAdvance() . 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: ThreadBlockedCount.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    final Phaser p = new Phaser(2);

    blocking = new BlockingThread(p);
    blocking.start();

    blocked = new BlockedThread(p);
    blocked.start();

    try {
        blocking.join();

        testOk = checkBlocked();
        p.arriveAndAwaitAdvance(); // #5

    } catch (InterruptedException e) {
        System.err.println("Unexpected exception.");
        e.printStackTrace(System.err);
        throw e;
    }
}
 
Example 2
Source File: ThreadBlockedCount.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    final Phaser p = new Phaser(2);

    blocking = new BlockingThread(p);
    blocking.start();

    blocked = new BlockedThread(p);
    blocked.start();

    try {
        blocking.join();

        testOk = checkBlocked();
        p.arriveAndAwaitAdvance(); // #5

    } catch (InterruptedException e) {
        System.err.println("Unexpected exception.");
        e.printStackTrace(System.err);
        throw e;
    }
}
 
Example 3
Source File: SynchronizationStatistics.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that blocking on a single monitor properly increases the
 * blocked count at least by 1. Also asserts that the correct lock name is provided.
 */
private static void testBlockingOnSimpleMonitor() throws Exception {
    System.out.println("testBlockingOnSimpleMonitor");
    final Object lock1 = new Object();
    System.out.println("Lock1 = " + lock1);

    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                p.arriveAndAwaitAdvance(); // phase[2]
            }
            p.arriveAndAwaitAdvance(); // phase[3]
        }
    });

    lt.start();
    long tid = lt.getId();
    ThreadInfo ti = mbean.getThreadInfo(tid);
    String lockName = null;
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); // phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
        lockName = mbean.getThreadInfo(tid).getLockName();
    }

    p.arriveAndAwaitAdvance(); // phase[2]
    testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);
    p.arriveAndDeregister(); // phase[3]

    lt.join();

    printok();
}
 
Example 4
Source File: SynchronizationStatistics.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that blocking on a single monitor properly increases the
 * blocked count at least by 1. Also asserts that the correct lock name is provided.
 */
private static void testBlockingOnSimpleMonitor() throws Exception {
    System.out.println("testBlockingOnSimpleMonitor");
    final Object lock1 = new Object();
    System.out.println("Lock1 = " + lock1);

    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                p.arriveAndAwaitAdvance(); // phase[2]
            }
            p.arriveAndAwaitAdvance(); // phase[3]
        }
    });

    lt.start();
    long tid = lt.getId();
    ThreadInfo ti = mbean.getThreadInfo(tid);
    String lockName = null;
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); // phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
        do {
            lockName = mbean.getThreadInfo(tid).getLockName();
        } while (lockName == null);
    }

    p.arriveAndAwaitAdvance(); // phase[2]
    testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);
    p.arriveAndDeregister(); // phase[3]

    lt.join();

    printok();
}
 
Example 5
Source File: SynchronizationStatistics.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that waiting on a single monitor properly increases the waited
 * count by 1 and the waited time by a positive number.
 */
private static void testWaitingOnSimpleMonitor() throws Exception {
    System.out.println("testWaitingOnSimpleMonitor");
    final Object lock1 = new Object();
    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                try {
                    lock1.wait(300);
                } catch (InterruptedException ex) {
                    // ignore
                }
                p.arriveAndAwaitAdvance(); // phase[2]
            }
            p.arriveAndAwaitAdvance(); // phase[3]
        }
    });

    lt.start();
    ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); // phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
    }
    p.arriveAndAwaitAdvance(); // phase[2]

    testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 1);
    p.arriveAndDeregister(); // phase[3]

    lt.join();

    printok();
}
 
Example 6
Source File: DoubleAdderDemo.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
static long timeTasks(Phaser phaser) {
    phaser.arriveAndAwaitAdvance();
    long start = System.nanoTime();
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    return System.nanoTime() - start;
}
 
Example 7
Source File: Basic.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
static void toTheStartingGate(Phaser gate) {
    try {
        gate.arriveAndAwaitAdvance();
    } catch (Throwable t) {
        unexpected(t);
    }
}
 
Example 8
Source File: LongAdderDemo.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static long timeTasks(Phaser phaser) {
    phaser.arriveAndAwaitAdvance();
    long start = System.nanoTime();
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    return System.nanoTime() - start;
}
 
Example 9
Source File: Basic.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void toTheStartingGate(Phaser gate) {
    try {
        gate.arriveAndAwaitAdvance();
    } catch (Throwable t) {
        unexpected(t);
    }
}
 
Example 10
Source File: SynchronizationStatistics.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that blocking on a single monitor properly increases the
 * blocked count at least by 1. Also asserts that the correct lock name is provided.
 */
private static void testBlockingOnSimpleMonitor() throws Exception {
    System.out.println("testBlockingOnSimpleMonitor");
    final Object lock1 = new Object();
    System.out.println("Lock1 = " + lock1);

    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                p.arriveAndAwaitAdvance(); // phase[2]
            }
            p.arriveAndAwaitAdvance(); // phase[3]
        }
    });

    lt.start();
    long tid = lt.getId();
    ThreadInfo ti = mbean.getThreadInfo(tid);
    String lockName = null;
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); // phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
        do {
            lockName = mbean.getThreadInfo(tid).getLockName();
        } while (lockName == null);
    }

    p.arriveAndAwaitAdvance(); // phase[2]
    testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);
    p.arriveAndDeregister(); // phase[3]

    lt.join();

    printok();
}
 
Example 11
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 12
Source File: Basic.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void toTheStartingGate(Phaser gate) {
    try {
        gate.arriveAndAwaitAdvance();
    } catch (Throwable t) {
        unexpected(t);
    }
}
 
Example 13
Source File: SynchronizationStatistics.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that waiting on a single monitor properly increases the waited
 * count by 1 and the waited time by a positive number.
 */
private static void testWaitingOnSimpleMonitor() throws Exception {
    System.out.println("testWaitingOnSimpleMonitor");
    final Object lock1 = new Object();
    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                try {
                    lock1.wait(300);
                } catch (InterruptedException ex) {
                    // ignore
                }
                p.arriveAndAwaitAdvance(); // phase[2]
            }
            p.arriveAndAwaitAdvance(); // phase[3]
        }
    });

    lt.start();
    ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); // phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
    }
    p.arriveAndAwaitAdvance(); // phase[2]

    testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 1);
    p.arriveAndDeregister(); // phase[3]

    lt.join();

    printok();
}
 
Example 14
Source File: Race.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    try (ServerSocket ss = new ServerSocket(0)) {
        final int port = ss.getLocalPort();
        final Phaser phaser = new Phaser(THREADS + 1);
        for (int i=0; i<100; i++) {
            try {
                final Socket s = new Socket("localhost", port);
                s.setSoLinger(false, 0);
                try (Socket sa = ss.accept()) {
                    sa.setSoLinger(false, 0);
                    final InputStream is = s.getInputStream();
                    Thread[] threads = new Thread[THREADS];
                    for (int j=0; j<THREADS; j++) {
                        threads[j] = new Thread() {
                        public void run() {
                            try {
                                phaser.arriveAndAwaitAdvance();
                                while (is.read() != -1)
                                    Thread.sleep(50);
                            } catch (Exception x) {
                                if (!(x instanceof SocketException
                                      && x.getMessage().equalsIgnoreCase("socket closed")))
                                    x.printStackTrace();
                                // ok, expect Socket closed
                            }
                        }};
                    }
                    for (int j=0; j<100; j++)
                        threads[j].start();
                    phaser.arriveAndAwaitAdvance();
                    s.close();
                    for (int j=0; j<100; j++)
                        threads[j].join();
                }
            } catch (ConnectException e) {
                System.err.println("Exception " + e + " Port: " + port);
            }
        }
    }
}
 
Example 15
Source File: Basic.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void toTheStartingGate(Phaser gate) {
    try {
        gate.arriveAndAwaitAdvance();
    } catch (Throwable t) {
        unexpected(t);
    }
}
 
Example 16
Source File: SynchronizationStatistics.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that waiting on a single monitor properly increases the waited
 * count by 1 and the waited time by a positive number.
 */
private static void testWaitingOnSimpleMonitor() throws Exception {
    System.out.println("testWaitingOnSimpleMonitor");
    final Object lock1 = new Object();
    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                try {
                    lock1.wait(300);
                } catch (InterruptedException ex) {
                    // ignore
                }
                p.arriveAndAwaitAdvance(); // phase[2]
            }
            p.arriveAndAwaitAdvance(); // phase[3]
        }
    });

    lt.start();
    ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); // phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
    }
    p.arriveAndAwaitAdvance(); // phase[2]

    testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 1);
    p.arriveAndDeregister(); // phase[3]

    lt.join();

    printok();
}
 
Example 17
Source File: SynchronizationStatistics.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests that waiting multiple times on the same monitor subsequently
 * increases the waited count by the number of subsequent calls and the
 * waited time by a positive number.
 */
private static void testMultiWaitingOnSimpleMonitor() throws Exception {
    System.out.println("testWaitingOnMultipleMonitors");
    final Object lock1 = new Object();

    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                for (int i = 0; i < 3; i++) {
                    try {
                        lock1.wait(300);
                    } catch (InterruptedException ex) {
                        // ignore
                    }
                    p.arriveAndAwaitAdvance(); // phase[2-4]
                }
            }
            p.arriveAndAwaitAdvance(); // phase[5]
        }
    });

    lt.start();
    ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); //phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
    }
    int phase = p.getPhase();
    while ((p.arriveAndAwaitAdvance() - phase) < 3); // phase[2-4]

    testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);
    p.arriveAndDeregister(); // phase[5]

    lt.join();

    printok();
}
 
Example 18
Source File: SynchronizationStatistics.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests that waiting multiple times on the same monitor subsequently
 * increases the waited count by the number of subsequent calls and the
 * waited time by a positive number.
 */
private static void testMultiWaitingOnSimpleMonitor() throws Exception {
    System.out.println("testWaitingOnMultipleMonitors");
    final Object lock1 = new Object();

    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                for (int i = 0; i < 3; i++) {
                    try {
                        lock1.wait(300);
                    } catch (InterruptedException ex) {
                        // ignore
                    }
                    p.arriveAndAwaitAdvance(); // phase[2-4]
                }
            }
            p.arriveAndAwaitAdvance(); // phase[5]
        }
    });

    lt.start();
    ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); //phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
    }
    int phase = p.getPhase();
    while ((p.arriveAndAwaitAdvance() - phase) < 3); // phase[2-4]

    testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);
    p.arriveAndDeregister(); // phase[5]

    lt.join();

    printok();
}
 
Example 19
Source File: SynchronizationStatistics.java    From native-obfuscator with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests that blocking on a nested monitor properly increases the
 * blocked count at least by 1 - it is not affected by the nesting depth.
 * Also asserts that the correct lock name is provided.
 */
private static void testBlockingOnNestedMonitor() throws Exception {
    final Object lock1 = new Object();
    final Object lock2 = new Object();

    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                p.arriveAndAwaitAdvance(); // phase[2]
                p.arriveAndAwaitAdvance(); // phase[3]
                synchronized(lock2) {
                    p.arriveAndAwaitAdvance(); // phase[4]
                }
                p.arriveAndAwaitAdvance(); // phase[5]
            }
        }
    });

    lt.start();
    long tid = lt.getId();
    ThreadInfo ti = mbean.getThreadInfo(tid);
    String lockName = null;
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); // phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
        do {
            lockName = mbean.getThreadInfo(tid).getLockName();
        } while (lockName == null);
    }
    p.arriveAndAwaitAdvance(); // phase[2]

    ti = testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);

    synchronized(lock2) {
        p.arriveAndAwaitAdvance(); // phase [3]
        waitForThreadState(lt, Thread.State.BLOCKED);
        do {
            lockName = mbean.getThreadInfo(tid).getLockName();
        } while (lockName == null);
    }
    p.arriveAndAwaitAdvance(); // phase [4]
    testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock2);
    p.arriveAndDeregister();

    lt.join();

    printok();
}
 
Example 20
Source File: SynchronizationStatistics.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests that waiting multiple times on the same monitor subsequently
 * increases the waited count by the number of subsequent calls and the
 * waited time by a positive number.
 */
private static void testMultiWaitingOnSimpleMonitor() throws Exception {
    System.out.println("testWaitingOnMultipleMonitors");
    final Object lock1 = new Object();

    final Phaser p = new Phaser(2);
    LockerThread lt = newLockerThread(new Runnable() {
        @Override
        public void run() {
            p.arriveAndAwaitAdvance(); // phase[1]
            synchronized(lock1) {
                System.out.println("[LockerThread obtained Lock1]");
                for (int i = 0; i < 3; i++) {
                    try {
                        lock1.wait(300);
                    } catch (InterruptedException ex) {
                        // ignore
                    }
                    p.arriveAndAwaitAdvance(); // phase[2-4]
                }
            }
            p.arriveAndAwaitAdvance(); // phase[5]
        }
    });

    lt.start();
    ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
    synchronized(lock1) {
        p.arriveAndAwaitAdvance(); //phase[1]
        waitForThreadState(lt, Thread.State.BLOCKED);
    }
    int phase = p.getPhase();
    while ((p.arriveAndAwaitAdvance() - phase) < 3); // phase[2-4]

    testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);
    p.arriveAndDeregister(); // phase[5]

    lt.join();

    printok();
}