abstractqueuedsynchronizertest.java

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

JAVA
1,301
字号
/*
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/licenses/publicdomain
 * Other contributors include Andrew Wright, Jeffrey Hayes,
 * Pat Fisher, Mike Judd.
 */


import junit.framework.*;
import edu.emory.mathcs.backport.java.util.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import edu.emory.mathcs.backport.java.util.concurrent.locks.*;
import java.io.*;

public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
//    public static void main(String[] args) {
//        junit.textui.TestRunner.run (suite());
//    }
//    public static Test suite() {
//        return new TestSuite(AbstractQueuedSynchronizerTest.class);
//    }
//
//    /**
//     * A simple mutex class, adapted from the
//     * AbstractQueuedSynchronizer javadoc.  Exclusive acquire tests
//     * exercise this as a sample user extension.  Other
//     * methods/features of AbstractQueuedSynchronizerTest are tested
//     * via other test classes, including those for ReentrantLock,
//     * ReentrantReadWriteLock, and Semaphore
//     */
//    static class Mutex extends AbstractQueuedSynchronizer {
//        public boolean isHeldExclusively() { return getState() == 1; }
//
//        public boolean tryAcquire(int acquires) {
//            assertTrue(acquires == 1);
//            return compareAndSetState(0, 1);
//        }
//
//        public boolean tryRelease(int releases) {
//            if (getState() == 0) throw new IllegalMonitorStateException();
//            setState(0);
//            return true;
//        }
//
//        public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
//
//    }
//
//
//    /**
//     * A simple latch class, to test shared mode.
//     */
//    static class BooleanLatch extends AbstractQueuedSynchronizer {
//        public boolean isSignalled() { return getState() != 0; }
//
//        public int tryAcquireShared(int ignore) {
//            return isSignalled()? 1 : -1;
//        }
//
//        public boolean tryReleaseShared(int ignore) {
//            setState(1);
//            return true;
//        }
//    }
//
//    /**
//     * A runnable calling acquireInterruptibly
//     */
//    class InterruptibleSyncRunnable implements Runnable {
//        final Mutex sync;
//        InterruptibleSyncRunnable(Mutex l) { sync = l; }
//        public void run() {
//            try {
//                sync.acquireInterruptibly(1);
//            } catch(InterruptedException success){}
//        }
//    }
//
//
//    /**
//     * A runnable calling acquireInterruptibly that expects to be
//     * interrupted
//     */
//    class InterruptedSyncRunnable implements Runnable {
//        final Mutex sync;
//        InterruptedSyncRunnable(Mutex l) { sync = l; }
//        public void run() {
//            try {
//                sync.acquireInterruptibly(1);
//                threadShouldThrow();
//            } catch(InterruptedException success){}
//        }
//    }
//
//    /**
//     * isHeldExclusively is false upon construction
//     */
//    public void testIsHeldExclusively() {
//	Mutex rl = new Mutex();
//        assertFalse(rl.isHeldExclusively());
//    }
//
//    /**
//     * acquiring released sync succeeds
//     */
//    public void testAcquire() {
//	Mutex rl = new Mutex();
//        rl.acquire(1);
//        assertTrue(rl.isHeldExclusively());
//        rl.release(1);
//        assertFalse(rl.isHeldExclusively());
//    }
//
//    /**
//     * tryAcquire on an released sync succeeds
//     */
//    public void testTryAcquire() {
//	Mutex rl = new Mutex();
//        assertTrue(rl.tryAcquire(1));
//        assertTrue(rl.isHeldExclusively());
//        rl.release(1);
//    }
//
//    /**
//     * hasQueuedThreads reports whether there are waiting threads
//     */
//    public void testhasQueuedThreads() {
//	final Mutex sync = new Mutex();
//        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
//        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
//        try {
//            assertFalse(sync.hasQueuedThreads());
//            sync.acquire(1);
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.hasQueuedThreads());
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.hasQueuedThreads());
//            t1.interrupt();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.hasQueuedThreads());
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(sync.hasQueuedThreads());
//            t1.join();
//            t2.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * isQueued(null) throws NPE
//     */
//    public void testIsQueuedNPE() {
//	final Mutex sync = new Mutex();
//        try {
//            sync.isQueued(null);
//            shouldThrow();
//        } catch (NullPointerException success) {
//        }
//    }
//
//    /**
//     * isQueued reports whether a thread is queued.
//     */
//    public void testIsQueued() {
//	final Mutex sync = new Mutex();
//        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
//        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
//        try {
//            assertFalse(sync.isQueued(t1));
//            assertFalse(sync.isQueued(t2));
//            sync.acquire(1);
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.isQueued(t1));
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.isQueued(t1));
//            assertTrue(sync.isQueued(t2));
//            t1.interrupt();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(sync.isQueued(t1));
//            assertTrue(sync.isQueued(t2));
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(sync.isQueued(t1));
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(sync.isQueued(t2));
//            t1.join();
//            t2.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getFirstQueuedThread returns first waiting thread or null if none
//     */
//    public void testGetFirstQueuedThread() {
//	final Mutex sync = new Mutex();
//        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
//        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
//        try {
//            assertNull(sync.getFirstQueuedThread());
//            sync.acquire(1);
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertEquals(t1, sync.getFirstQueuedThread());
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertEquals(t1, sync.getFirstQueuedThread());
//            t1.interrupt();
//            Thread.sleep(SHORT_DELAY_MS);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertEquals(t2, sync.getFirstQueuedThread());
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertNull(sync.getFirstQueuedThread());
//            t1.join();
//            t2.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * hasContended reports false if no thread has ever blocked, else true
//     */
//    public void testHasContended() {
//	final Mutex sync = new Mutex();
//        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
//        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
//        try {
//            assertFalse(sync.hasContended());
//            sync.acquire(1);
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.hasContended());
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.hasContended());
//            t1.interrupt();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.hasContended());
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.hasContended());
//            t1.join();
//            t2.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getQueuedThreads includes waiting threads
//     */
//    public void testGetQueuedThreads() {
//	final Mutex sync = new Mutex();
//        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
//        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
//        try {
//            assertTrue(sync.getQueuedThreads().isEmpty());
//            sync.acquire(1);
//            assertTrue(sync.getQueuedThreads().isEmpty());
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getQueuedThreads().contains(t1));
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getQueuedThreads().contains(t1));
//            assertTrue(sync.getQueuedThreads().contains(t2));
//            t1.interrupt();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(sync.getQueuedThreads().contains(t1));
//            assertTrue(sync.getQueuedThreads().contains(t2));
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getQueuedThreads().isEmpty());
//            t1.join();
//            t2.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getExclusiveQueuedThreads includes waiting threads
//     */
//    public void testGetExclusiveQueuedThreads() {
//	final Mutex sync = new Mutex();
//        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
//        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
//        try {
//            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
//            sync.acquire(1);
//            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
//            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
//            t1.interrupt();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
//            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
//            t1.join();
//            t2.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * getSharedQueuedThreads does not include exclusively waiting threads
//     */
//    public void testGetSharedQueuedThreads() {
//	final Mutex sync = new Mutex();
//        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
//        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
//        try {
//            assertTrue(sync.getSharedQueuedThreads().isEmpty());
//            sync.acquire(1);
//            assertTrue(sync.getSharedQueuedThreads().isEmpty());
//            t1.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getSharedQueuedThreads().isEmpty());
//            t2.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getSharedQueuedThreads().isEmpty());
//            t1.interrupt();
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getSharedQueuedThreads().isEmpty());
//            sync.release(1);
//            Thread.sleep(SHORT_DELAY_MS);
//            assertTrue(sync.getSharedQueuedThreads().isEmpty());
//            t1.join();
//            t2.join();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * tryAcquireNanos is interruptible.
//     */
//    public void testInterruptedException2() {
//	final Mutex sync = new Mutex();
//	sync.acquire(1);
//	Thread t = new Thread(new Runnable() {
//                public void run() {
//                    try {
//			sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
//			threadShouldThrow();
//		    } catch(InterruptedException success){}
//		}
//	    });
//        try {
//            t.start();
//            t.interrupt();
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * TryAcquire on exclusively held sync fails
//     */
//    public void testTryAcquireWhenSynced() {
//	final Mutex sync = new Mutex();
//	sync.acquire(1);
//	Thread t = new Thread(new Runnable() {
//                public void run() {
//                    threadAssertFalse(sync.tryAcquire(1));
//		}
//	    });
//        try {
//            t.start();
//            t.join();
//            sync.release(1);
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//    /**
//     * tryAcquireNanos on an exclusively held sync times out
//     */
//    public void testAcquireNanos_Timeout() {
//	final Mutex sync = new Mutex();
//	sync.acquire(1);
//	Thread t = new Thread(new Runnable() {
//                public void run() {
//		    try {
//                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
//                    } catch (Exception ex) {
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//        try {
//            t.start();
//            t.join();
//            sync.release(1);
//        } catch(Exception e){
//            unexpectedException();
//        }
//    }
//
//
//    /**
//     * getState is true when acquired and false when not
//     */
//    public void testGetState() {
//	final Mutex sync = new Mutex();
//	sync.acquire(1);
//	assertTrue(sync.isHeldExclusively());
//	sync.release(1);
//	assertFalse(sync.isHeldExclusively());
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    sync.acquire(1);
//		    try {

⌨️ 快捷键说明

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