reentrantlocktest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,089 行 · 第 1/3 页
JAVA
1,089 行
// }
// }
// });
//
// try {
// t.start();
// Thread.sleep(SHORT_DELAY_MS);
// lock.lock();
// assertTrue(lock.hasWaiters(c));
// assertEquals(1, lock.getWaitQueueLength(c));
// c.signal();
// lock.unlock();
// Thread.sleep(SHORT_DELAY_MS);
// lock.lock();
// assertFalse(lock.hasWaiters(c));
// assertEquals(0, lock.getWaitQueueLength(c));
// lock.unlock();
// t.join(SHORT_DELAY_MS);
// assertFalse(t.isAlive());
// }
// catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitQueueLength returns number of waiting threads
// */
// public void testGetWaitQueueLength() {
// final ReentrantLock lock = new ReentrantLock();
// final Condition c = lock.newCondition();
// Thread t1 = 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();
// }
// }
// });
//
// Thread t2 = new Thread(new Runnable() {
// public void run() {
// try {
// lock.lock();
// threadAssertTrue(lock.hasWaiters(c));
// threadAssertEquals(1, lock.getWaitQueueLength(c));
// c.await();
// lock.unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
// }
// }
// });
//
// try {
// t1.start();
// Thread.sleep(SHORT_DELAY_MS);
// t2.start();
// Thread.sleep(SHORT_DELAY_MS);
// lock.lock();
// assertTrue(lock.hasWaiters(c));
// assertEquals(2, lock.getWaitQueueLength(c));
// c.signalAll();
// lock.unlock();
// Thread.sleep(SHORT_DELAY_MS);
// lock.lock();
// assertFalse(lock.hasWaiters(c));
// assertEquals(0, lock.getWaitQueueLength(c));
// lock.unlock();
// t1.join(SHORT_DELAY_MS);
// t2.join(SHORT_DELAY_MS);
// assertFalse(t1.isAlive());
// assertFalse(t2.isAlive());
// }
// catch (Exception ex) {
// unexpectedException();
// }
// }
// /**
// * getWaitingThreads returns only and all waiting threads
// */
// public void testGetWaitingThreads() {
// final PublicReentrantLock lock = new PublicReentrantLock();
// final Condition c = lock.newCondition();
// Thread t1 = new Thread(new Runnable() {
// public void run() {
// try {
// lock.lock();
// threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
// c.await();
// lock.unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
// }
// }
// });
//
// Thread t2 = new Thread(new Runnable() {
// public void run() {
// try {
// lock.lock();
// threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
// c.await();
// lock.unlock();
// }
// catch(InterruptedException e) {
// threadUnexpectedException();
// }
// }
// });
//
// try {
// lock.lock();
// assertTrue(lock.getWaitingThreads(c).isEmpty());
// lock.unlock();
// t1.start();
// Thread.sleep(SHORT_DELAY_MS);
// t2.start();
// Thread.sleep(SHORT_DELAY_MS);
// lock.lock();
// assertTrue(lock.hasWaiters(c));
// assertTrue(lock.getWaitingThreads(c).contains(t1));
// assertTrue(lock.getWaitingThreads(c).contains(t2));
// c.signalAll();
// lock.unlock();
// Thread.sleep(SHORT_DELAY_MS);
// lock.lock();
// assertFalse(lock.hasWaiters(c));
// assertTrue(lock.getWaitingThreads(c).isEmpty());
// lock.unlock();
// 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 ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
lock.lock();
c.awaitUninterruptibly();
lock.unlock();
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
lock.lock();
c.signal();
lock.unlock();
assert(t.isInterrupted());
t.join(SHORT_DELAY_MS);
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* await is interruptible
*/
public void testAwait_Interrupt() {
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();
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 ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
lock.lock();
c.await(1000, TimeUnit.MILLISECONDS); // 1 sec
lock.unlock();
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 ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
lock.lock();
java.util.Date d = new java.util.Date();
c.awaitUntil(new java.util.Date(d.getTime() + 10000));
lock.unlock();
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 ReentrantLock lock = new ReentrantLock();
final Condition c = lock.newCondition();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
lock.lock();
c.await();
lock.unlock();
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
lock.lock();
c.await();
lock.unlock();
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
try {
t1.start();
t2.start();
Thread.sleep(SHORT_DELAY_MS);
lock.lock();
c.signalAll();
lock.unlock();
t1.join(SHORT_DELAY_MS);
t2.join(SHORT_DELAY_MS);
assertFalse(t1.isAlive());
assertFalse(t2.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* A serialized lock deserializes as unlocked
*/
public void testSerialization() {
ReentrantLock l = new ReentrantLock();
l.lock();
l.unlock();
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));
ReentrantLock r = (ReentrantLock) in.readObject();
r.lock();
r.unlock();
} catch(Exception e){
e.printStackTrace();
unexpectedException();
}
}
/**
* toString indicates current lock state
*/
public void testToString() {
ReentrantLock lock = new ReentrantLock();
String us = lock.toString();
assertTrue(us.indexOf("Unlocked") >= 0);
lock.lock();
String ls = lock.toString();
assertTrue(ls.indexOf("Locked") >= 0);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?