reentrantreadwritelocktest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,422 行 · 第 1/3 页
JAVA
1,422 行
}
}
// /**
// * hasQueuedThreads reports whether there are waiting threads
// */
// public void testhasQueuedThreads() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// Thread t1 = new Thread(new InterruptedLockRunnable(lock));
// Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
// try {
// assertFalse(lock.hasQueuedThreads());
// lock.writeLock().lock();
// t1.start();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(lock.hasQueuedThreads());
// t2.start();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(lock.hasQueuedThreads());
// t1.interrupt();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(lock.hasQueuedThreads());
// lock.writeLock().unlock();
// Thread.sleep(SHORT_DELAY_MS);
// assertFalse(lock.hasQueuedThreads());
// t1.join();
// t2.join();
// } catch(Exception e){
// unexpectedException();
// }
// }
//
// /**
// * hasQueuedThread(null) throws NPE
// */
// public void testHasQueuedThreadNPE() {
// final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
// try {
// sync.hasQueuedThread(null);
// shouldThrow();
// } catch (NullPointerException success) {
// }
// }
// /**
// * hasQueuedThread reports whether a thread is queued.
// */
// public void testHasQueuedThread() {
// final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
// Thread t1 = new Thread(new InterruptedLockRunnable(sync));
// Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
// try {
// assertFalse(sync.hasQueuedThread(t1));
// assertFalse(sync.hasQueuedThread(t2));
// sync.writeLock().lock();
// t1.start();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(sync.hasQueuedThread(t1));
// t2.start();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(sync.hasQueuedThread(t1));
// assertTrue(sync.hasQueuedThread(t2));
// t1.interrupt();
// Thread.sleep(SHORT_DELAY_MS);
// assertFalse(sync.hasQueuedThread(t1));
// assertTrue(sync.hasQueuedThread(t2));
// sync.writeLock().unlock();
// Thread.sleep(SHORT_DELAY_MS);
// assertFalse(sync.hasQueuedThread(t1));
// Thread.sleep(SHORT_DELAY_MS);
// assertFalse(sync.hasQueuedThread(t2));
// t1.join();
// t2.join();
// } catch(Exception e){
// unexpectedException();
// }
// }
// /**
// * getQueueLength reports number of waiting threads
// */
// public void testGetQueueLength() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// Thread t1 = new Thread(new InterruptedLockRunnable(lock));
// Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
// try {
// assertEquals(0, lock.getQueueLength());
// lock.writeLock().lock();
// 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.writeLock().unlock();
// Thread.sleep(SHORT_DELAY_MS);
// assertEquals(0, lock.getQueueLength());
// t1.join();
// t2.join();
// } catch(Exception e){
// unexpectedException();
// }
// }
// /**
// * getQueuedThreads includes waiting threads
// */
// public void testGetQueuedThreads() {
// final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
// Thread t1 = new Thread(new InterruptedLockRunnable(lock));
// Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
// try {
// assertTrue(lock.getQueuedThreads().isEmpty());
// lock.writeLock().lock();
// assertTrue(lock.getQueuedThreads().isEmpty());
// t1.start();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(lock.getQueuedThreads().contains(t1));
// t2.start();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(lock.getQueuedThreads().contains(t1));
// assertTrue(lock.getQueuedThreads().contains(t2));
// t1.interrupt();
// Thread.sleep(SHORT_DELAY_MS);
// assertFalse(lock.getQueuedThreads().contains(t1));
// assertTrue(lock.getQueuedThreads().contains(t2));
// lock.writeLock().unlock();
// Thread.sleep(SHORT_DELAY_MS);
// assertTrue(lock.getQueuedThreads().isEmpty());
// t1.join();
// t2.join();
// } catch(Exception e){
// unexpectedException();
// }
// }
// /**
// * hasWaiters throws NPE if null
// */
// public void testHasWaitersNPE() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// try {
// lock.hasWaiters(null);
// shouldThrow();
// } catch (NullPointerException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength throws NPE if null
// */
// public void testGetWaitQueueLengthNPE() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// try {
// lock.getWaitQueueLength(null);
// shouldThrow();
// } catch (NullPointerException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads throws NPE if null
// */
// public void testGetWaitingThreadsNPE() {
// final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
// try {
// lock.getWaitingThreads(null);
// shouldThrow();
// } catch (NullPointerException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * hasWaiters throws IAE if not owned
// */
// public void testHasWaitersIAE() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
// try {
// lock2.hasWaiters(c);
// shouldThrow();
// } catch (IllegalArgumentException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * hasWaiters throws IMSE if not locked
// */
// public void testHasWaitersIMSE() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// try {
// lock.hasWaiters(c);
// shouldThrow();
// } catch (IllegalMonitorStateException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength throws IAE if not owned
// */
// public void testGetWaitQueueLengthIAE() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
// try {
// lock2.getWaitQueueLength(c);
// shouldThrow();
// } catch (IllegalArgumentException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength throws IMSE if not locked
// */
// public void testGetWaitQueueLengthIMSE() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// try {
// lock.getWaitQueueLength(c);
// shouldThrow();
// } catch (IllegalMonitorStateException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads throws IAE if not owned
// */
// public void testGetWaitingThreadsIAE() {
// final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
// try {
// lock2.getWaitingThreads(c);
// shouldThrow();
// } catch (IllegalArgumentException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads throws IMSE if not locked
// */
// public void testGetWaitingThreadsIMSE() {
// final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// try {
// lock.getWaitingThreads(c);
// shouldThrow();
// } catch (IllegalMonitorStateException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * hasWaiters returns true when a thread is waiting, else false
// */
// public void testHasWaiters() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// Thread t = new Thread(new Runnable() {
// public void run() {
// try {
// lock.writeLock().lock();
// threadAssertFalse(lock.hasWaiters(c));
// threadAssertEquals(0, lock.getWaitQueueLength(c));
// c.await();
// lock.writeLock().unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
// }
// }
// });
//
// try {
// t.start();
// Thread.sleep(SHORT_DELAY_MS);
// lock.writeLock().lock();
// assertTrue(lock.hasWaiters(c));
// assertEquals(1, lock.getWaitQueueLength(c));
// c.signal();
// lock.writeLock().unlock();
// Thread.sleep(SHORT_DELAY_MS);
// lock.writeLock().lock();
// assertFalse(lock.hasWaiters(c));
// assertEquals(0, lock.getWaitQueueLength(c));
// lock.writeLock().unlock();
// t.join(SHORT_DELAY_MS);
// assertFalse(t.isAlive());
// }
// catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength returns number of waiting threads
// */
// public void testGetWaitQueueLength() {
// final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
// final Condition c = (lock.writeLock().newCondition());
// Thread t = new Thread(new Runnable() {
// public void run() {
// try {
// lock.writeLock().lock();
// threadAssertFalse(lock.hasWaiters(c));
// threadAssertEquals(0, lock.getWaitQueueLength(c));
// c.await();
// lock.writeLock().unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
// }
// }
// });
//
// try {
// t.start();
// Thread.sleep(SHORT_DELAY_MS);
// lock.writeLock().lock();
// assertTrue(lock.hasWaiters(c));
// assertEquals(1, lock.getWaitQueueLength(c));
// c.signal();
// lock.writeLock().unlock();
// Thread.sleep(SHORT_DELAY_MS);
// lock.writeLock().lock();
// assertFalse(lock.hasWaiters(c));
// assertEquals(0, lock.getWaitQueueLength(c));
// lock.writeLock().unlock();
// t.join(SHORT_DELAY_MS);
// assertFalse(t.isAlive());
// }
// catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads returns only and all waiting threads
// */
// public void testGetWaitingThreads() {
// final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
// final Condition c = lock.writeLock().newCondition();
// Thread t1 = new Thread(new Runnable() {
// public void run() {
// try {
// lock.writeLock().lock();
// threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
// c.await();
// lock.writeLock().unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
// }
// }
// });
//
// Thread t2 = new Thread(new Runnable() {
// public void run() {
// try {
// lock.writeLock().lock();
// threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
// c.await();
// lock.writeLock().unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
// }
// }
// });
//
// try {
// lock.writeLock().lock();
// assertTrue(lock.getWaitingThreads(c).isEmpty());
// lock.writeLock().unlock();
// t1.start();
// Thread.sleep(SHORT_DELAY_MS);
// t2.start();
// Thread.sleep(SHORT_DELAY_MS);
// lock.writeLock().lock();
// assertTrue(lock.hasWaiters(c));
// assertTrue(lock.getWaitingThreads(c).contains(t1));
// assertTrue(lock.getWaitingThreads(c).contains(t2));
// c.signalAll();
// lock.writeLock().unlock();
// Thread.sleep(SHORT_DELAY_MS);
// lock.writeLock().lock();
// assertFalse(lock.hasWaiters(c));
// assertTrue(lock.getWaitingThreads(c).isEmpty());
// lock.writeLock().unlock();
// t1.join(SHORT_DELAY_MS);
// t2.join(SHORT_DELAY_MS);
// assertFalse(t1.isAlive());
// assertFalse(t2.isAlive());
// }
// catch (Exception ex) {
// unexpectedException();
// }
// }
/**
* toString indicates current lock state
*/
public void testToString() {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
String us = lock.toString();
assertTrue(us.indexOf("Write locks = 0") >= 0);
assertTrue(us.indexOf("Read locks = 0") >= 0);
lock.writeLock().lock();
String ws = lock.toString();
assertTrue(ws.indexOf("Write locks = 1") >= 0);
assertTrue(ws.indexOf("Read locks = 0") >= 0);
lock.writeLock().unlock();
lock.readLock().lock();
lock.readLock().lock();
String rs = lock.toString();
assertTrue(rs.indexOf("Write locks = 0") >= 0);
assertTrue(rs.indexOf("Read locks = 2") >= 0);
}
/**
* readLock.toString indicates current lock state
*/
public void testReadLockToString() {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
String us = lock.readLock().toString();
assertTrue(us.indexOf("Read locks = 0") >= 0);
lock.readLock().lock();
lock.readLock().lock();
String rs = lock.readLock().toString();
assertTrue(rs.indexOf("Read locks = 2") >= 0);
}
/**
* writeLock.toString indicates current lock state
*/
public void testWriteLockToString() {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
String us = lock.writeLock().toString();
assertTrue(us.indexOf("Unlocked") >= 0);
lock.writeLock().lock();
String ls = lock.writeLock().toString();
assertTrue(ls.indexOf("Locked") >= 0);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?