abstractqueuedsynchronizertest.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,301 行 · 第 1/3 页

JAVA
1,301
字号
//			Thread.sleep(SMALL_DELAY_MS);
//		    }
//		    catch(Exception e) {
//                        threadUnexpectedException();
//                    }
//		    sync.release(1);
//		}
//	    });
//	try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.isHeldExclusively());
//            t.join();
//            assertFalse(sync.isHeldExclusively());
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * acquireInterruptibly is interruptible.
//     */
//    public void testAcquireInterruptibly1() {
//	final Mutex sync = new Mutex();
//	sync.acquire(1);
//	Thread t = new Thread(new InterruptedSyncRunnable(sync));
//        try {
//            t.start();
//            t.interrupt();
//            sync.release(1);
//            t.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * acquireInterruptibly succeeds when released, else is interruptible
//     */
//    public void testAcquireInterruptibly2() {
//	final Mutex sync = new Mutex();
//	try {
//            sync.acquireInterruptibly(1);
//        } catch(Exception e) {
//            unexpectedException();
//        }
//	Thread t = new Thread(new InterruptedSyncRunnable(sync));
//        try {
//            t.start();
//            t.interrupt();
//            assertTrue(sync.isHeldExclusively());
//            t.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * owns is true for a condition created by sync else false
//     */
//    public void testOwns() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//        final Mutex sync2 = new Mutex();
//        assertTrue(sync.owns(c));
//        assertFalse(sync2.owns(c));
//    }
//
//    /**
//     * Calling await without holding sync throws IllegalMonitorStateException
//     */
//    public void testAwait_IllegalMonitor() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//        try {
//            c.await();
//            shouldThrow();
//        }
//        catch (IllegalMonitorStateException success) {
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * Calling signal without holding sync throws IllegalMonitorStateException
//     */
//    public void testSignal_IllegalMonitor() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//        try {
//            c.signal();
//            shouldThrow();
//        }
//        catch (IllegalMonitorStateException success) {
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * awaitNanos without a signal times out
//     */
//    public void testAwaitNanos_Timeout() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//        try {
//            sync.acquire(1);
//            long t = c.awaitNanos(100);
//            assertTrue(t <= 0);
//            sync.release(1);
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     *  Timed await without a signal times out
//     */
//    public void testAwait_Timeout() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//        try {
//            sync.acquire(1);
//            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
//            sync.release(1);
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * awaitUntil without a signal times out
//     */
//    public void testAwaitUntil_Timeout() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//        try {
//            sync.acquire(1);
//            edu.emory.mathcs.backport.java.util.Date d = new edu.emory.mathcs.backport.java.util.Date();
//            assertFalse(c.awaitUntil(new edu.emory.mathcs.backport.java.util.Date(d.getTime() + 10)));
//            sync.release(1);
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * await returns when signalled
//     */
//    public void testAwait() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            c.signal();
//            sync.release(1);
//            t.join(SHORT_DELAY_MS);
//            assertFalse(t.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//
//    /**
//     * hasWaiters throws NPE if null
//     */
//    public void testHasWaitersNPE() {
//	final Mutex sync = new Mutex();
//        try {
//            sync.hasWaiters(null);
//            shouldThrow();
//        } catch (NullPointerException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getWaitQueueLength throws NPE if null
//     */
//    public void testGetWaitQueueLengthNPE() {
//	final Mutex sync = new Mutex();
//        try {
//            sync.getWaitQueueLength(null);
//            shouldThrow();
//        } catch (NullPointerException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * getWaitingThreads throws NPE if null
//     */
//    public void testGetWaitingThreadsNPE() {
//	final Mutex sync = new Mutex();
//        try {
//            sync.getWaitingThreads(null);
//            shouldThrow();
//        } catch (NullPointerException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * hasWaiters throws IAE if not owned
//     */
//    public void testHasWaitersIAE() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
//	final Mutex sync2 = new Mutex();
//        try {
//            sync2.hasWaiters(c);
//            shouldThrow();
//        } catch (IllegalArgumentException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * hasWaiters throws IMSE if not synced
//     */
//    public void testHasWaitersIMSE() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
//        try {
//            sync.hasWaiters(c);
//            shouldThrow();
//        } catch (IllegalMonitorStateException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * getWaitQueueLength throws IAE if not owned
//     */
//    public void testGetWaitQueueLengthIAE() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
//	final Mutex sync2 = new Mutex();
//        try {
//            sync2.getWaitQueueLength(c);
//            shouldThrow();
//        } catch (IllegalArgumentException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getWaitQueueLength throws IMSE if not synced
//     */
//    public void testGetWaitQueueLengthIMSE() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
//        try {
//            sync.getWaitQueueLength(c);
//            shouldThrow();
//        } catch (IllegalMonitorStateException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * getWaitingThreads throws IAE if not owned
//     */
//    public void testGetWaitingThreadsIAE() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
//	final Mutex sync2 = new Mutex();
//        try {
//            sync2.getWaitingThreads(c);
//            shouldThrow();
//        } catch (IllegalArgumentException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getWaitingThreads throws IMSE if not synced
//     */
//    public void testGetWaitingThreadsIMSE() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
//        try {
//            sync.getWaitingThreads(c);
//            shouldThrow();
//        } catch (IllegalMonitorStateException success) {
//        } catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//
//    /**
//     * hasWaiters returns true when a thread is waiting, else false
//     */
//    public void testHasWaiters() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        threadAssertFalse(sync.hasWaiters(c));
//                        threadAssertEquals(0, sync.getWaitQueueLength(c));
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            assertTrue(sync.hasWaiters(c));
//            assertEquals(1, sync.getWaitQueueLength(c));
//            c.signal();
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            assertFalse(sync.hasWaiters(c));
//            assertEquals(0, sync.getWaitQueueLength(c));
//            sync.release(1);
//            t.join(SHORT_DELAY_MS);
//            assertFalse(t.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getWaitQueueLength returns number of waiting threads
//     */
//    public void testGetWaitQueueLength() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//	Thread t1 = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        threadAssertFalse(sync.hasWaiters(c));
//                        threadAssertEquals(0, sync.getWaitQueueLength(c));
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//	Thread t2 = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        threadAssertTrue(sync.hasWaiters(c));
//                        threadAssertEquals(1, sync.getWaitQueueLength(c));
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//        try {
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            assertTrue(sync.hasWaiters(c));
//            assertEquals(2, sync.getWaitQueueLength(c));
//            c.signalAll();
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            assertFalse(sync.hasWaiters(c));
//            assertEquals(0, sync.getWaitQueueLength(c));
//            sync.release(1);
//            t1.join(SHORT_DELAY_MS);
//            t2.join(SHORT_DELAY_MS);
//            assertFalse(t1.isAlive());
//            assertFalse(t2.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getWaitingThreads returns only and all waiting threads

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?