176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
+ |
* getQueueLength reports number of waiting threads |
180 |
+ |
*/ |
181 |
+ |
public void testGetQueueLength_fair() { |
182 |
+ |
final ReentrantLock lock = new ReentrantLock(true); |
183 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
184 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
185 |
+ |
try { |
186 |
+ |
assertEquals(0, lock.getQueueLength()); |
187 |
+ |
lock.lock(); |
188 |
+ |
t1.start(); |
189 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
190 |
+ |
assertEquals(1, lock.getQueueLength()); |
191 |
+ |
t2.start(); |
192 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
193 |
+ |
assertEquals(2, lock.getQueueLength()); |
194 |
+ |
t1.interrupt(); |
195 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
196 |
+ |
assertEquals(1, lock.getQueueLength()); |
197 |
+ |
lock.unlock(); |
198 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
199 |
+ |
assertEquals(0, lock.getQueueLength()); |
200 |
+ |
t1.join(); |
201 |
+ |
t2.join(); |
202 |
+ |
} catch(Exception e){ |
203 |
+ |
unexpectedException(); |
204 |
+ |
} |
205 |
+ |
} |
206 |
+ |
|
207 |
+ |
/** |
208 |
+ |
* hasQueuedThread(null) throws NPE |
209 |
+ |
*/ |
210 |
+ |
public void testHasQueuedThreadNPE() { |
211 |
+ |
final ReentrantLock sync = new ReentrantLock(); |
212 |
+ |
try { |
213 |
+ |
sync.hasQueuedThread(null); |
214 |
+ |
shouldThrow(); |
215 |
+ |
} catch (NullPointerException success) { |
216 |
+ |
} |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
/** |
220 |
+ |
* hasQueuedThread reports whether a thread is queued. |
221 |
+ |
*/ |
222 |
+ |
public void testHasQueuedThread() { |
223 |
+ |
final ReentrantLock sync = new ReentrantLock(); |
224 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
225 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
226 |
+ |
try { |
227 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
228 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
229 |
+ |
sync.lock(); |
230 |
+ |
t1.start(); |
231 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
232 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
233 |
+ |
t2.start(); |
234 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
235 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
236 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
237 |
+ |
t1.interrupt(); |
238 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
239 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
240 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
241 |
+ |
sync.unlock(); |
242 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
243 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
244 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
245 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
246 |
+ |
t1.join(); |
247 |
+ |
t2.join(); |
248 |
+ |
} catch(Exception e){ |
249 |
+ |
unexpectedException(); |
250 |
+ |
} |
251 |
+ |
} |
252 |
+ |
|
253 |
+ |
|
254 |
+ |
/** |
255 |
|
* getQueuedThreads includes waiting threads |
256 |
|
*/ |
257 |
|
public void testGetQueuedThreads() { |
409 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
410 |
|
try { |
411 |
|
t.start(); |
412 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
413 |
|
t.interrupt(); |
414 |
|
lock.unlock(); |
415 |
|
t.join(); |
499 |
|
final Condition c = lock.newCondition(); |
500 |
|
try { |
501 |
|
lock.lock(); |
502 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
502 |
> |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
503 |
|
lock.unlock(); |
504 |
|
} |
505 |
|
catch (Exception ex) { |
516 |
|
try { |
517 |
|
lock.lock(); |
518 |
|
java.util.Date d = new java.util.Date(); |
519 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
519 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10)); |
520 |
|
lock.unlock(); |
521 |
|
} |
522 |
|
catch (Exception ex) { |
864 |
|
} |
865 |
|
} |
866 |
|
|
867 |
< |
|
867 |
> |
/** A helper class for uninterruptible wait tests */ |
868 |
> |
class UninterruptableThread extends Thread { |
869 |
> |
private ReentrantLock lock; |
870 |
> |
private Condition c; |
871 |
> |
|
872 |
> |
public volatile boolean canAwake = false; |
873 |
> |
public volatile boolean interrupted = false; |
874 |
> |
public volatile boolean lockStarted = false; |
875 |
> |
|
876 |
> |
public UninterruptableThread(ReentrantLock lock, Condition c) { |
877 |
> |
this.lock = lock; |
878 |
> |
this.c = c; |
879 |
> |
} |
880 |
> |
|
881 |
> |
public synchronized void run() { |
882 |
> |
lock.lock(); |
883 |
> |
lockStarted = true; |
884 |
> |
|
885 |
> |
while (!canAwake) { |
886 |
> |
c.awaitUninterruptibly(); |
887 |
> |
} |
888 |
> |
|
889 |
> |
interrupted = isInterrupted(); |
890 |
> |
lock.unlock(); |
891 |
> |
} |
892 |
> |
} |
893 |
|
|
894 |
|
/** |
895 |
|
* awaitUninterruptibly doesn't abort on interrupt |
896 |
|
*/ |
897 |
|
public void testAwaitUninterruptibly() { |
898 |
< |
final ReentrantLock lock = new ReentrantLock(); |
898 |
> |
final ReentrantLock lock = new ReentrantLock(); |
899 |
|
final Condition c = lock.newCondition(); |
900 |
< |
Thread t = new Thread(new Runnable() { |
799 |
< |
public void run() { |
800 |
< |
lock.lock(); |
801 |
< |
c.awaitUninterruptibly(); |
802 |
< |
lock.unlock(); |
803 |
< |
} |
804 |
< |
}); |
900 |
> |
UninterruptableThread thread = new UninterruptableThread(lock, c); |
901 |
|
|
902 |
|
try { |
903 |
< |
t.start(); |
904 |
< |
Thread.sleep(SHORT_DELAY_MS); |
905 |
< |
t.interrupt(); |
903 |
> |
thread.start(); |
904 |
> |
|
905 |
> |
while (!thread.lockStarted) { |
906 |
> |
Thread.sleep(100); |
907 |
> |
} |
908 |
> |
|
909 |
|
lock.lock(); |
910 |
< |
c.signal(); |
911 |
< |
lock.unlock(); |
912 |
< |
assert(t.isInterrupted()); |
913 |
< |
t.join(SHORT_DELAY_MS); |
914 |
< |
assertFalse(t.isAlive()); |
915 |
< |
} |
916 |
< |
catch (Exception ex) { |
910 |
> |
try { |
911 |
> |
thread.interrupt(); |
912 |
> |
thread.canAwake = true; |
913 |
> |
c.signal(); |
914 |
> |
} finally { |
915 |
> |
lock.unlock(); |
916 |
> |
} |
917 |
> |
|
918 |
> |
thread.join(); |
919 |
> |
assertTrue(thread.interrupted); |
920 |
> |
assertFalse(thread.isAlive()); |
921 |
> |
} catch (Exception ex) { |
922 |
|
unexpectedException(); |
923 |
|
} |
924 |
|
} |
964 |
|
public void run() { |
965 |
|
try { |
966 |
|
lock.lock(); |
967 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
967 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
968 |
|
lock.unlock(); |
969 |
|
threadShouldThrow(); |
970 |
|
} |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1070 |
+ |
* await after multiple reentrant locking preserves lock count |
1071 |
+ |
*/ |
1072 |
+ |
public void testAwaitLockCount() { |
1073 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
1074 |
+ |
final Condition c = lock.newCondition(); |
1075 |
+ |
Thread t1 = new Thread(new Runnable() { |
1076 |
+ |
public void run() { |
1077 |
+ |
try { |
1078 |
+ |
lock.lock(); |
1079 |
+ |
threadAssertEquals(1, lock.getHoldCount()); |
1080 |
+ |
c.await(); |
1081 |
+ |
threadAssertEquals(1, lock.getHoldCount()); |
1082 |
+ |
lock.unlock(); |
1083 |
+ |
} |
1084 |
+ |
catch(InterruptedException e) { |
1085 |
+ |
threadUnexpectedException(); |
1086 |
+ |
} |
1087 |
+ |
} |
1088 |
+ |
}); |
1089 |
+ |
|
1090 |
+ |
Thread t2 = new Thread(new Runnable() { |
1091 |
+ |
public void run() { |
1092 |
+ |
try { |
1093 |
+ |
lock.lock(); |
1094 |
+ |
lock.lock(); |
1095 |
+ |
threadAssertEquals(2, lock.getHoldCount()); |
1096 |
+ |
c.await(); |
1097 |
+ |
threadAssertEquals(2, lock.getHoldCount()); |
1098 |
+ |
lock.unlock(); |
1099 |
+ |
lock.unlock(); |
1100 |
+ |
} |
1101 |
+ |
catch(InterruptedException e) { |
1102 |
+ |
threadUnexpectedException(); |
1103 |
+ |
} |
1104 |
+ |
} |
1105 |
+ |
}); |
1106 |
+ |
|
1107 |
+ |
try { |
1108 |
+ |
t1.start(); |
1109 |
+ |
t2.start(); |
1110 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1111 |
+ |
lock.lock(); |
1112 |
+ |
c.signalAll(); |
1113 |
+ |
lock.unlock(); |
1114 |
+ |
t1.join(SHORT_DELAY_MS); |
1115 |
+ |
t2.join(SHORT_DELAY_MS); |
1116 |
+ |
assertFalse(t1.isAlive()); |
1117 |
+ |
assertFalse(t2.isAlive()); |
1118 |
+ |
} |
1119 |
+ |
catch (Exception ex) { |
1120 |
+ |
unexpectedException(); |
1121 |
+ |
} |
1122 |
+ |
} |
1123 |
+ |
|
1124 |
+ |
/** |
1125 |
|
* A serialized lock deserializes as unlocked |
1126 |
|
*/ |
1127 |
|
public void testSerialization() { |
1146 |
|
} |
1147 |
|
} |
1148 |
|
|
1149 |
+ |
/** |
1150 |
+ |
* toString indicates current lock state |
1151 |
+ |
*/ |
1152 |
+ |
public void testToString() { |
1153 |
+ |
ReentrantLock lock = new ReentrantLock(); |
1154 |
+ |
String us = lock.toString(); |
1155 |
+ |
assertTrue(us.indexOf("Unlocked") >= 0); |
1156 |
+ |
lock.lock(); |
1157 |
+ |
String ls = lock.toString(); |
1158 |
+ |
assertTrue(ls.indexOf("Locked") >= 0); |
1159 |
+ |
} |
1160 |
+ |
|
1161 |
|
} |