semaphoretest.java

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

JAVA
927
字号
                    }
                }
            });
        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            s.release();
            s.release();
            s.release();
            s.release();
            s.release();
            s.release();
            t.join();
            assertEquals(2, s.availablePermits());
        } catch( InterruptedException e){
            unexpectedException();
        }
    }

//    /**
//     * release(n) in one thread enables acquire(n) in another thread
//     */
//    public void testAcquireReleaseNInDifferentThreads_fair() {
//        final Semaphore s = new Semaphore(0, true);
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			s.acquire();
//                        s.release(2);
//                        s.acquire();
//		    } catch(InterruptedException ie){
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            s.release(2);
//            s.acquire(2);
//            s.release(1);
//            t.join();
//	} catch( InterruptedException e){
//            unexpectedException();
//        }
//    }

//    /**
//     * release(n) in one thread enables acquire(n) in another thread
//     */
//    public void testAcquireReleaseNInDifferentThreads_fair2() {
//        final Semaphore s = new Semaphore(0, true);
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//                        s.acquire(2);
//                        s.acquire(2);
//                        s.release(4);
//		    } catch(InterruptedException ie){
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//        try {
//            t.start();
//            Thread.sleep(SHORT_DELAY_MS);
//            s.release(6);
//            s.acquire(2);
//            s.acquire(2);
//            s.release(2);
//            t.join();
//	} catch( InterruptedException e){
//            unexpectedException();
//        }
//    }





    /**
     * release in one thread enables timed acquire in another thread
     */
    public void testTimedAcquireReleaseInDifferentThreads_fair() {
        final Semaphore s = new Semaphore(1, true);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));

                    } catch(InterruptedException ie){
                        threadUnexpectedException();
                    }
                }
            });
        t.start();
        try {
            s.release();
            s.release();
            s.release();
            s.release();
            s.release();
            t.join();
        } catch( InterruptedException e){
            unexpectedException();
        }
    }

//    /**
//     * release(n) in one thread enables timed acquire(n) in another thread
//     */
//    public void testTimedAcquireReleaseNInDifferentThreads_fair() {
//        final Semaphore s = new Semaphore(2, true);
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
//                        s.release(2);
//                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
//                        s.release(2);
//		    } catch(InterruptedException ie){
//                        threadUnexpectedException();
//                    }
//		}
//	    });
//	t.start();
//        try {
//            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
//            s.release(2);
//            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
//            s.release(2);
//            t.join();
//	} catch( InterruptedException e){
//            unexpectedException();
//        }
//    }

    /**
     * A waiting acquire blocks interruptibly
     */
    public void testAcquire_InterruptedException_fair() {
        final Semaphore s = new Semaphore(0, true);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        s.acquire();
                        threadShouldThrow();
                    } catch(InterruptedException success){}
                }
            });
        t.start();
        try {
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(InterruptedException e){
            unexpectedException();
        }
    }

//    /**
//     * A waiting acquire(n) blocks interruptibly
//     */
//    public void testAcquireN_InterruptedException_fair() {
//	final Semaphore s = new Semaphore(2, true);
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			s.acquire(3);
//			threadShouldThrow();
//		    } catch(InterruptedException success){}
//		}
//	    });
//	t.start();
//	try {
//	    Thread.sleep(SHORT_DELAY_MS);
//            t.interrupt();
//            t.join();
//        } catch(InterruptedException e){
//            unexpectedException();
//        }
//    }

    /**
     *  A waiting tryAcquire blocks interruptibly
     */
    public void testTryAcquire_InterruptedException_fair() {
        final Semaphore s = new Semaphore(0, true);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
                        threadShouldThrow();
                    } catch(InterruptedException success){
                    }
                }
            });
        t.start();
        try {
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(InterruptedException e){
            unexpectedException();
        }
    }

//    /**
//     *  A waiting tryAcquire(n) blocks interruptibly
//     */
//    public void testTryAcquireN_InterruptedException_fair() {
//	final Semaphore s = new Semaphore(1, true);
//	Thread t = new Thread(new Runnable() {
//		public void run() {
//		    try {
//			s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
//			threadShouldThrow();
//		    } catch(InterruptedException success){
//                    }
//		}
//	    });
//	t.start();
//	try {
//	    Thread.sleep(SHORT_DELAY_MS);
//            t.interrupt();
//            t.join();
//        } catch(InterruptedException e){
//            unexpectedException();
//        }
//    }

    /**
     * getQueueLength reports number of waiting threads
     */
    public void testGetQueueLength_fair() {
        final Semaphore lock = new Semaphore(1, true);
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertEquals(0, lock.getQueueLength());
            lock.acquireUninterruptibly();
            t1.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(1, lock.getQueueLength());
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(2, lock.getQueueLength());
            t1.interrupt();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(1, lock.getQueueLength());
            lock.release();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(0, lock.getQueueLength());
            t1.join();
            t2.join();
        } catch(Exception e){
            unexpectedException();
        }
    }


    /**
     * a deserialized serialized semaphore has same number of permits
     */
    public void testSerialization_fair() {
        Semaphore l = new Semaphore(3, true);

        try {
            l.acquire();
            l.release();
            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));
            Semaphore r = (Semaphore) in.readObject();
            assertEquals(3, r.availablePermits());
            assertTrue(r.isFair());
            r.acquire();
            r.release();
        } catch(Exception e){
            e.printStackTrace();
            unexpectedException();
        }
    }

    /**
     * toString indicates current number of permits
     */
    public void testToString() {
        Semaphore s = new Semaphore(0);
        String us = s.toString();
        assertTrue(us.indexOf("Permits = 0") >= 0);
        s.release();
        String s1 = s.toString();
        assertTrue(s1.indexOf("Permits = 1") >= 0);
        s.release();
        String s2 = s.toString();
        assertTrue(s2.indexOf("Permits = 2") >= 0);
    }

}

⌨️ 快捷键说明

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