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

The following examples show how to use java.util.concurrent.Phaser#arriveAndDeregister() . 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: SynchronizationStatistics.java    From TencentKona-8 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 2
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 3
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 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: PhaserTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * arriveAndDeregister() throws IllegalStateException if number of
 * registered or unarrived parties would become negative
 */
public void testArriveAndDeregister1() {
    Phaser phaser = new Phaser();
    try {
        phaser.arriveAndDeregister();
        shouldThrow();
    } catch (IllegalStateException success) {}
}
 
Example 6
Source File: SynchronizationStatistics.java    From native-obfuscator with GNU General Public License v3.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 {
    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) {
                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 7
Source File: SynchronizationStatistics.java    From jdk8u-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 8
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 9
Source File: SynchronizationStatistics.java    From openjdk-jdk9 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 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: SynchronizationStatistics.java    From dragonwell8_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 12
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 13
Source File: TestTaskExecutor.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test(invocationCount = 100)
public void testLevelMovement()
{
    TestingTicker ticker = new TestingTicker();
    TaskExecutor taskExecutor = new TaskExecutor(2, 2, 3, 4, ticker);
    taskExecutor.start();
    ticker.increment(20, MILLISECONDS);

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

        Phaser globalPhaser = new Phaser();
        globalPhaser.bulkRegister(3); // 2 taskExecutor threads + test thread

        int quantaTimeMills = 500;
        int phasesPerSecond = 1000 / quantaTimeMills;
        int totalPhases = LEVEL_THRESHOLD_SECONDS[LEVEL_THRESHOLD_SECONDS.length - 1] * phasesPerSecond;
        TestingJob driver1 = new TestingJob(ticker, globalPhaser, new Phaser(), new Phaser(), totalPhases, quantaTimeMills);
        TestingJob driver2 = new TestingJob(ticker, globalPhaser, new Phaser(), new Phaser(), totalPhases, quantaTimeMills);

        taskExecutor.enqueueSplits(testTaskHandle, true, ImmutableList.of(driver1, driver2));

        int completedPhases = 0;
        for (int i = 0; i < (LEVEL_THRESHOLD_SECONDS.length - 1); i++) {
            for (; (completedPhases / phasesPerSecond) < LEVEL_THRESHOLD_SECONDS[i + 1]; completedPhases++) {
                globalPhaser.arriveAndAwaitAdvance();
            }

            assertEquals(testTaskHandle.getPriority().getLevel(), i + 1);
        }

        globalPhaser.arriveAndDeregister();
    }
    finally {
        taskExecutor.stop();
    }
}
 
Example 14
Source File: SynchronizationStatistics.java    From jdk8u-jdk with GNU General Public License v2.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 {
    System.out.println("testBlockingOnNestedMonitor");
    final Object lock1 = new Object();
    final Object lock2 = new Object();

    System.out.println("Lock1 = " + lock1);
    System.out.println("Lock2 = " + lock2);

    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]
                synchronized(lock2) {
                    System.out.println("[LockerThread obtained 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 15
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 16
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 17
Source File: SynchronizationStatistics.java    From jdk8u-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 dragonwell8_jdk with GNU General Public License v2.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 {
    System.out.println("testBlockingOnNestedMonitor");
    final Object lock1 = new Object();
    final Object lock2 = new Object();

    System.out.println("Lock1 = " + lock1);
    System.out.println("Lock2 = " + lock2);

    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]
                synchronized(lock2) {
                    System.out.println("[LockerThread obtained 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 19
Source File: SynchronizationStatistics.java    From openjdk-jdk9 with GNU General Public License v2.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 {
    System.out.println("testBlockingOnNestedMonitor");
    final Object lock1 = new Object();
    final Object lock2 = new Object();

    System.out.println("Lock1 = " + lock1);
    System.out.println("Lock2 = " + lock2);

    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]
                synchronized(lock2) {
                    System.out.println("[LockerThread obtained 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: TestTaskExecutor.java    From presto with Apache License 2.0 4 votes vote down vote up
@Test(invocationCount = 100)
public void testLevelMultipliers()
        throws Exception
{
    TestingTicker ticker = new TestingTicker();
    TaskExecutor taskExecutor = new TaskExecutor(6, 3, 3, 4, new MultilevelSplitQueue(2), ticker);
    taskExecutor.start();
    ticker.increment(20, MILLISECONDS);

    try {
        for (int i = 0; i < (LEVEL_THRESHOLD_SECONDS.length - 1); i++) {
            TaskHandle[] taskHandles = {
                    taskExecutor.addTask(new TaskId("test1", 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty()),
                    taskExecutor.addTask(new TaskId("test2", 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty()),
                    taskExecutor.addTask(new TaskId("test3", 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty())
            };

            // move task 0 to next level
            TestingJob task0Job = new TestingJob(ticker, new Phaser(), new Phaser(), new Phaser(), 1, LEVEL_THRESHOLD_SECONDS[i + 1] * 1000);
            taskExecutor.enqueueSplits(
                    taskHandles[0],
                    true,
                    ImmutableList.of(task0Job));
            // move tasks 1 and 2 to this level
            TestingJob task1Job = new TestingJob(ticker, new Phaser(), new Phaser(), new Phaser(), 1, LEVEL_THRESHOLD_SECONDS[i] * 1000);
            taskExecutor.enqueueSplits(
                    taskHandles[1],
                    true,
                    ImmutableList.of(task1Job));
            TestingJob task2Job = new TestingJob(ticker, new Phaser(), new Phaser(), new Phaser(), 1, LEVEL_THRESHOLD_SECONDS[i] * 1000);
            taskExecutor.enqueueSplits(
                    taskHandles[2],
                    true,
                    ImmutableList.of(task2Job));

            task0Job.getCompletedFuture().get();
            task1Job.getCompletedFuture().get();
            task2Job.getCompletedFuture().get();

            // then, start new drivers for all tasks
            Phaser globalPhaser = new Phaser(7); // 6 taskExecutor threads + test thread
            int phasesForNextLevel = LEVEL_THRESHOLD_SECONDS[i + 1] - LEVEL_THRESHOLD_SECONDS[i];
            TestingJob[] drivers = new TestingJob[6];
            for (int j = 0; j < 6; j++) {
                drivers[j] = new TestingJob(ticker, globalPhaser, new Phaser(), new Phaser(), phasesForNextLevel, 1000);
            }

            taskExecutor.enqueueSplits(taskHandles[0], true, ImmutableList.of(drivers[0], drivers[1]));
            taskExecutor.enqueueSplits(taskHandles[1], true, ImmutableList.of(drivers[2], drivers[3]));
            taskExecutor.enqueueSplits(taskHandles[2], true, ImmutableList.of(drivers[4], drivers[5]));

            // run all three drivers
            int lowerLevelStart = drivers[2].getCompletedPhases() + drivers[3].getCompletedPhases() + drivers[4].getCompletedPhases() + drivers[5].getCompletedPhases();
            int higherLevelStart = drivers[0].getCompletedPhases() + drivers[1].getCompletedPhases();
            while (Arrays.stream(drivers).noneMatch(TestingJob::isFinished)) {
                globalPhaser.arriveAndAwaitAdvance();

                int lowerLevelEnd = drivers[2].getCompletedPhases() + drivers[3].getCompletedPhases() + drivers[4].getCompletedPhases() + drivers[5].getCompletedPhases();
                int lowerLevelTime = lowerLevelEnd - lowerLevelStart;
                int higherLevelEnd = drivers[0].getCompletedPhases() + drivers[1].getCompletedPhases();
                int higherLevelTime = higherLevelEnd - higherLevelStart;

                if (higherLevelTime > 20) {
                    assertGreaterThan(lowerLevelTime, (higherLevelTime * 2) - 10);
                    assertLessThan(higherLevelTime, (lowerLevelTime * 2) + 10);
                }
            }

            globalPhaser.arriveAndDeregister();
            taskExecutor.removeTask(taskHandles[0]);
            taskExecutor.removeTask(taskHandles[1]);
            taskExecutor.removeTask(taskHandles[2]);
        }
    }
    finally {
        taskExecutor.stop();
    }
}