reentrantlocktest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,089 行 · 第 1/3 页
JAVA
1,089 行
*/
public void testGetHoldCount() {
ReentrantLock lock = new ReentrantLock();
for(int i = 1; i <= SIZE; i++) {
lock.lock();
assertEquals(i,lock.getHoldCount());
}
for(int i = SIZE; i > 0; i--) {
lock.unlock();
assertEquals(i-1,lock.getHoldCount());
}
}
/**
* isLocked is true when locked and false when not
*/
public void testIsLocked() {
final ReentrantLock lock = new ReentrantLock();
lock.lock();
assertTrue(lock.isLocked());
lock.unlock();
assertFalse(lock.isLocked());
Thread t = new Thread(new Runnable() {
public void run() {
lock.lock();
try {
Thread.sleep(SMALL_DELAY_MS);
}
catch(Exception e) {
threadUnexpectedException();
}
lock.unlock();
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(lock.isLocked());
t.join();
assertFalse(lock.isLocked());
} catch(Exception e){
unexpectedException();
}
}
/**
* lockInterruptibly is interruptible.
*/
public void testLockInterruptibly1() {
final ReentrantLock lock = new ReentrantLock();
lock.lock();
Thread t = new Thread(new InterruptedLockRunnable(lock));
try {
t.start();
t.interrupt();
lock.unlock();
t.join();
} catch(Exception e){
unexpectedException();
}
}
/**
* lockInterruptibly succeeds when unlocked, else is interruptible
*/
public void testLockInterruptibly2() {
final ReentrantLock lock = new ReentrantLock();
try {
lock.lockInterruptibly();
} catch(Exception e) {
unexpectedException();
}
Thread t = new Thread(new InterruptedLockRunnable(lock));
try {
t.start();
t.interrupt();
assertTrue(lock.isLocked());
assertTrue(lock.isHeldByCurrentThread());
t.join();
} catch(Exception e){
unexpectedException();
}
}
/**
* Calling await without holding lock throws IllegalMonitorStateException
*/
public void testAwait_IllegalMonitor() {
final ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
try {
c.await();
shouldThrow();
}
catch (IllegalMonitorStateException success) {
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* Calling signal without holding lock throws IllegalMonitorStateException
*/
public void testSignal_IllegalMonitor() {
final ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
try {
c.signal();
shouldThrow();
}
catch (IllegalMonitorStateException success) {
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* awaitNanos without a signal times out
*/
public void testAwaitNanos_Timeout() {
final ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
try {
lock.lock();
long deadline = System.currentTimeMillis() + 1;
c.await(1, TimeUnit.MILLISECONDS);
long t = deadline - System.currentTimeMillis();
assertTrue(t <= 0);
lock.unlock();
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* timed await without a signal times out
*/
public void testAwait_Timeout() {
final ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
try {
lock.lock();
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
lock.unlock();
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* awaitUntil without a signal times out
*/
public void testAwaitUntil_Timeout() {
final ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
try {
lock.lock();
java.util.Date d = new java.util.Date();
c.awaitUntil(new java.util.Date(d.getTime() + 10));
lock.unlock();
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* await returns when signalled
*/
public void testAwait() {
final ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
lock.lock();
c.await();
lock.unlock();
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
lock.lock();
c.signal();
lock.unlock();
t.join(SHORT_DELAY_MS);
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
}
// /**
// * hasWaiters throws NPE if null
// */
// public void testHasWaitersNPE() {
// final ReentrantLock lock = new ReentrantLock();
// try {
// lock.hasWaiters(null);
// shouldThrow();
// } catch (NullPointerException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength throws NPE if null
// */
// public void testGetWaitQueueLengthNPE() {
// final ReentrantLock lock = new ReentrantLock();
// try {
// lock.getWaitQueueLength(null);
// shouldThrow();
// } catch (NullPointerException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads throws NPE if null
// */
// public void testGetWaitingThreadsNPE() {
// final PublicReentrantLock lock = new PublicReentrantLock();
// try {
// lock.getWaitingThreads(null);
// shouldThrow();
// } catch (NullPointerException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * hasWaiters throws IAE if not owned
// */
// public void testHasWaitersIAE() {
// final ReentrantLock lock = new ReentrantLock();
// final Condition c = (lock.newCondition());
// final ReentrantLock lock2 = new ReentrantLock();
// try {
// lock2.hasWaiters(c);
// shouldThrow();
// } catch (IllegalArgumentException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * hasWaiters throws IMSE if not locked
// */
// public void testHasWaitersIMSE() {
// final ReentrantLock lock = new ReentrantLock();
// final Condition c = (lock.newCondition());
// try {
// lock.hasWaiters(c);
// shouldThrow();
// } catch (IllegalMonitorStateException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength throws IAE if not owned
// */
// public void testGetWaitQueueLengthIAE() {
// final ReentrantLock lock = new ReentrantLock();
// final Condition c = (lock.newCondition());
// final ReentrantLock lock2 = new ReentrantLock();
// try {
// lock2.getWaitQueueLength(c);
// shouldThrow();
// } catch (IllegalArgumentException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength throws IMSE if not locked
// */
// public void testGetWaitQueueLengthIMSE() {
// final ReentrantLock lock = new ReentrantLock();
// final Condition c = (lock.newCondition());
// try {
// lock.getWaitQueueLength(c);
// shouldThrow();
// } catch (IllegalMonitorStateException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads throws IAE if not owned
// */
// public void testGetWaitingThreadsIAE() {
// final PublicReentrantLock lock = new PublicReentrantLock();
// final Condition c = (lock.newCondition());
// final PublicReentrantLock lock2 = new PublicReentrantLock();
// try {
// lock2.getWaitingThreads(c);
// shouldThrow();
// } catch (IllegalArgumentException success) {
// } catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads throws IMSE if not locked
// */
// public void testGetWaitingThreadsIMSE() {
// final PublicReentrantLock lock = new PublicReentrantLock();
// final Condition c = (lock.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 ReentrantLock lock = new ReentrantLock();
// final Condition c = lock.newCondition();
// Thread t = new Thread(new Runnable() {
// public void run() {
// try {
// lock.lock();
// threadAssertFalse(lock.hasWaiters(c));
// threadAssertEquals(0, lock.getWaitQueueLength(c));
// c.await();
// lock.unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?