abstractqueuedsynchronizertest.java

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

JAVA
1,301
字号
//     */
//    public void testGetWaitingThreads() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//	Thread t1 = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//	Thread t2 = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//        try {
//            sync.acquire(1);
//            assertTrue(sync.getWaitingThreads(c).isEmpty());
//            sync.release(1);
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            assertTrue(sync.hasWaiters(c));
//            assertTrue(sync.getWaitingThreads(c).contains(t1));
//            assertTrue(sync.getWaitingThreads(c).contains(t2));
//            c.signalAll();
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            assertFalse(sync.hasWaiters(c));
//            assertTrue(sync.getWaitingThreads(c).isEmpty());
//            sync.release(1);
//            t1.join(SHORT_DELAY_MS);
//            t2.join(SHORT_DELAY_MS);
//            assertFalse(t1.isAlive());
//            assertFalse(t2.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//
//    /**
//     * awaitUninterruptibly doesn't abort on interrupt
//     */
//    public void testAwaitUninterruptibly() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//                    sync.acquire(1);
//                    c.awaitUninterruptibly();
//                    sync.release(1);
//		}
//	    });
//
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            t.interrupt();
//            sync.acquire(1);
//            c.signal();
//            sync.release(1);
//            assert(t.isInterrupted());
//            t.join(SHORT_DELAY_MS);
//            assertFalse(t.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * await is interruptible
//     */
//    public void testAwait_Interrupt() {
//	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);
//                        threadShouldThrow();
//		    }
//		    catch(InterruptedException success) {
//                    }
//		}
//	    });
//
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            t.interrupt();
//            t.join(SHORT_DELAY_MS);
//            assertFalse(t.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * awaitNanos is interruptible
//     */
//    public void testAwaitNanos_Interrupt() {
//	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.awaitNanos(1000 * 1000 * 1000); // 1 sec
//                        sync.release(1);
//                        threadShouldThrow();
//		    }
//		    catch(InterruptedException success) {
//                    }
//		}
//	    });
//
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            t.interrupt();
//            t.join(SHORT_DELAY_MS);
//            assertFalse(t.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * awaitUntil is interruptible
//     */
//    public void testAwaitUntil_Interrupt() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        edu.emory.mathcs.backport.java.util.Date d = new edu.emory.mathcs.backport.java.util.Date();
//                        c.awaitUntil(new edu.emory.mathcs.backport.java.util.Date(d.getTime() + 10000));
//                        sync.release(1);
//                        threadShouldThrow();
//		    }
//		    catch(InterruptedException success) {
//                    }
//		}
//	    });
//
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            t.interrupt();
//            t.join(SHORT_DELAY_MS);
//            assertFalse(t.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//    /**
//     * signalAll wakes up all threads
//     */
//    public void testSignalAll() {
//	final Mutex sync = new Mutex();
//        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
//	Thread t1 = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//	Thread t2 = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			sync.acquire(1);
//                        c.await();
//                        sync.release(1);
//		    }
//		    catch(InterruptedException e) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//
//        try {
//            t1.start();
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            sync.acquire(1);
//            c.signalAll();
//            sync.release(1);
//            t1.join(SHORT_DELAY_MS);
//            t2.join(SHORT_DELAY_MS);
//            assertFalse(t1.isAlive());
//            assertFalse(t2.isAlive());
//        }
//        catch (Exception ex) {
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * toString indicates current state
//     */
//    public void testToString() {
//        Mutex sync = new Mutex();
//        String us = sync.toString();
//        assertTrue(us.indexOf("State = 0") >= 0);
//        sync.acquire(1);
//        String ls = sync.toString();
//        assertTrue(ls.indexOf("State = 1") >= 0);
//    }
//
//    /**
//     * A serialized AQS deserializes with current state
//     */
//    public void testSerialization() {
//        Mutex l = new Mutex();
//        l.acquire(1);
//        assertTrue(l.isHeldExclusively());
//
//        try {
//            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
//            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
//            out.writeObject(l);
//            out.close();
//
//            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
//            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
//            Mutex r = (Mutex) in.readObject();
//            assertTrue(r.isHeldExclusively());
//        } catch(Exception e){
//            e.printStackTrace();
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * tryReleaseShared setting state changes getState
//     */
//    public void testGetStateWithReleaseShared() {
//	final BooleanLatch l = new BooleanLatch();
//	assertFalse(l.isSignalled());
//	l.releaseShared(0);
//	assertTrue(l.isSignalled());
//    }
//
//    /**
//     * releaseShared has no effect when already signalled
//     */
//    public void testReleaseShared() {
//	final BooleanLatch l = new BooleanLatch();
//	assertFalse(l.isSignalled());
//	l.releaseShared(0);
//	assertTrue(l.isSignalled());
//	l.releaseShared(0);
//	assertTrue(l.isSignalled());
//    }
//
//    /**
//     * acquireSharedInterruptibly returns after release, but not before
//     */
//    public void testAcquireSharedInterruptibly() {
//	final BooleanLatch l = new BooleanLatch();
//
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//                        threadAssertFalse(l.isSignalled());
//			l.acquireSharedInterruptibly(0);
//                        threadAssertTrue(l.isSignalled());
//		    } catch(InterruptedException e){
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//	try {
//            t.start();
//            assertFalse(l.isSignalled());
//            Thread.sleep(SHORT_DELAY_MS);
//            l.releaseShared(0);
//            assertTrue(l.isSignalled());
//            t.join();
//        } catch (InterruptedException e){
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * acquireSharedTimed returns after release
//     */
//    public void testAsquireSharedTimed() {
//	final BooleanLatch l = new BooleanLatch();
//
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//                        threadAssertFalse(l.isSignalled());
//			threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
//                        threadAssertTrue(l.isSignalled());
//
//		    } catch(InterruptedException e){
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//	try {
//            t.start();
//            assertFalse(l.isSignalled());
//            Thread.sleep(SHORT_DELAY_MS);
//            l.releaseShared(0);
//            assertTrue(l.isSignalled());
//            t.join();
//        } catch (InterruptedException e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * acquireSharedInterruptibly throws IE if interrupted before released
//     */
//    public void testAcquireSharedInterruptibly_InterruptedException() {
//        final BooleanLatch l = new BooleanLatch();
//        Thread t = new Thread(new Runnable() {
//                public void run() {
//                    try {
//                        threadAssertFalse(l.isSignalled());
//                        l.acquireSharedInterruptibly(0);
//                        threadShouldThrow();
//                    } catch(InterruptedException success){}
//                }
//            });
//	t.start();
//	try {
//            assertFalse(l.isSignalled());
//            t.interrupt();
//            t.join();
//        } catch (InterruptedException e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * acquireSharedTimed throws IE if interrupted before released
//     */
//    public void testAcquireSharedNanos_InterruptedException() {
//        final BooleanLatch l = new BooleanLatch();
//        Thread t = new Thread(new Runnable() {
//                public void run() {
//                    try {
//                        threadAssertFalse(l.isSignalled());
//                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
//                        threadShouldThrow();
//                    } catch(InterruptedException success){}
//                }
//            });
//        t.start();
//        try {
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(l.isSignalled());
//            t.interrupt();
//            t.join();
//        } catch (InterruptedException e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * acquireSharedTimed times out if not released before timeout
//     */
//    public void testAcquireSharedNanos_Timeout() {
//        final BooleanLatch l = new BooleanLatch();
//        Thread t = new Thread(new Runnable() {
//                public void run() {
//                    try {
//                        threadAssertFalse(l.isSignalled());
//                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
//                    } catch(InterruptedException ie){
//                        threadUnexpectedException();
//                    }
//                }
//            });
//        t.start();
//        try {
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(l.isSignalled());
//            t.join();
//        } catch (InterruptedException e){
//            unexpectedException();
//        }
//    }
//
//
}

⌨️ 快捷键说明

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