409 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
410 |
|
try { |
411 |
|
t.start(); |
412 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
413 |
|
t.interrupt(); |
414 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
415 |
|
lock.unlock(); |
416 |
|
t.join(); |
417 |
|
} catch(Exception e){ |
500 |
|
final Condition c = lock.newCondition(); |
501 |
|
try { |
502 |
|
lock.lock(); |
503 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
503 |
> |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
504 |
|
lock.unlock(); |
505 |
|
} |
506 |
|
catch (Exception ex) { |
517 |
|
try { |
518 |
|
lock.lock(); |
519 |
|
java.util.Date d = new java.util.Date(); |
520 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
520 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10)); |
521 |
|
lock.unlock(); |
522 |
|
} |
523 |
|
catch (Exception ex) { |
865 |
|
} |
866 |
|
} |
867 |
|
|
868 |
< |
|
868 |
> |
/** A helper class for uninterruptible wait tests */ |
869 |
> |
class UninterruptableThread extends Thread { |
870 |
> |
private ReentrantLock lock; |
871 |
> |
private Condition c; |
872 |
> |
|
873 |
> |
public volatile boolean canAwake = false; |
874 |
> |
public volatile boolean interrupted = false; |
875 |
> |
public volatile boolean lockStarted = false; |
876 |
> |
|
877 |
> |
public UninterruptableThread(ReentrantLock lock, Condition c) { |
878 |
> |
this.lock = lock; |
879 |
> |
this.c = c; |
880 |
> |
} |
881 |
> |
|
882 |
> |
public synchronized void run() { |
883 |
> |
lock.lock(); |
884 |
> |
lockStarted = true; |
885 |
> |
|
886 |
> |
while (!canAwake) { |
887 |
> |
c.awaitUninterruptibly(); |
888 |
> |
} |
889 |
> |
|
890 |
> |
interrupted = isInterrupted(); |
891 |
> |
lock.unlock(); |
892 |
> |
} |
893 |
> |
} |
894 |
|
|
895 |
|
/** |
896 |
|
* awaitUninterruptibly doesn't abort on interrupt |
897 |
|
*/ |
898 |
|
public void testAwaitUninterruptibly() { |
899 |
< |
final ReentrantLock lock = new ReentrantLock(); |
899 |
> |
final ReentrantLock lock = new ReentrantLock(); |
900 |
|
final Condition c = lock.newCondition(); |
901 |
< |
Thread t = new Thread(new Runnable() { |
875 |
< |
public void run() { |
876 |
< |
lock.lock(); |
877 |
< |
c.awaitUninterruptibly(); |
878 |
< |
lock.unlock(); |
879 |
< |
} |
880 |
< |
}); |
901 |
> |
UninterruptableThread thread = new UninterruptableThread(lock, c); |
902 |
|
|
903 |
|
try { |
904 |
< |
t.start(); |
905 |
< |
Thread.sleep(SHORT_DELAY_MS); |
906 |
< |
t.interrupt(); |
904 |
> |
thread.start(); |
905 |
> |
|
906 |
> |
while (!thread.lockStarted) { |
907 |
> |
Thread.sleep(100); |
908 |
> |
} |
909 |
> |
|
910 |
|
lock.lock(); |
911 |
< |
c.signal(); |
912 |
< |
lock.unlock(); |
913 |
< |
assert(t.isInterrupted()); |
914 |
< |
t.join(SHORT_DELAY_MS); |
915 |
< |
assertFalse(t.isAlive()); |
916 |
< |
} |
917 |
< |
catch (Exception ex) { |
911 |
> |
try { |
912 |
> |
thread.interrupt(); |
913 |
> |
thread.canAwake = true; |
914 |
> |
c.signal(); |
915 |
> |
} finally { |
916 |
> |
lock.unlock(); |
917 |
> |
} |
918 |
> |
|
919 |
> |
thread.join(); |
920 |
> |
assertTrue(thread.interrupted); |
921 |
> |
assertFalse(thread.isAlive()); |
922 |
> |
} catch (Exception ex) { |
923 |
|
unexpectedException(); |
924 |
|
} |
925 |
|
} |
1045 |
|
lock.unlock(); |
1046 |
|
} |
1047 |
|
catch(InterruptedException e) { |
1048 |
+ |
threadUnexpectedException(); |
1049 |
+ |
} |
1050 |
+ |
} |
1051 |
+ |
}); |
1052 |
+ |
|
1053 |
+ |
try { |
1054 |
+ |
t1.start(); |
1055 |
+ |
t2.start(); |
1056 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1057 |
+ |
lock.lock(); |
1058 |
+ |
c.signalAll(); |
1059 |
+ |
lock.unlock(); |
1060 |
+ |
t1.join(SHORT_DELAY_MS); |
1061 |
+ |
t2.join(SHORT_DELAY_MS); |
1062 |
+ |
assertFalse(t1.isAlive()); |
1063 |
+ |
assertFalse(t2.isAlive()); |
1064 |
+ |
} |
1065 |
+ |
catch (Exception ex) { |
1066 |
+ |
unexpectedException(); |
1067 |
+ |
} |
1068 |
+ |
} |
1069 |
+ |
|
1070 |
+ |
/** |
1071 |
+ |
* await after multiple reentrant locking preserves lock count |
1072 |
+ |
*/ |
1073 |
+ |
public void testAwaitLockCount() { |
1074 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
1075 |
+ |
final Condition c = lock.newCondition(); |
1076 |
+ |
Thread t1 = new Thread(new Runnable() { |
1077 |
+ |
public void run() { |
1078 |
+ |
try { |
1079 |
+ |
lock.lock(); |
1080 |
+ |
threadAssertEquals(1, lock.getHoldCount()); |
1081 |
+ |
c.await(); |
1082 |
+ |
threadAssertEquals(1, lock.getHoldCount()); |
1083 |
+ |
lock.unlock(); |
1084 |
+ |
} |
1085 |
+ |
catch(InterruptedException e) { |
1086 |
+ |
threadUnexpectedException(); |
1087 |
+ |
} |
1088 |
+ |
} |
1089 |
+ |
}); |
1090 |
+ |
|
1091 |
+ |
Thread t2 = new Thread(new Runnable() { |
1092 |
+ |
public void run() { |
1093 |
+ |
try { |
1094 |
+ |
lock.lock(); |
1095 |
+ |
lock.lock(); |
1096 |
+ |
threadAssertEquals(2, lock.getHoldCount()); |
1097 |
+ |
c.await(); |
1098 |
+ |
threadAssertEquals(2, lock.getHoldCount()); |
1099 |
+ |
lock.unlock(); |
1100 |
+ |
lock.unlock(); |
1101 |
+ |
} |
1102 |
+ |
catch(InterruptedException e) { |
1103 |
|
threadUnexpectedException(); |
1104 |
|
} |
1105 |
|
} |