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