6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.HashSet; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
|
import java.util.concurrent.locks.Condition; |
17 |
|
import java.util.concurrent.locks.Lock; |
18 |
|
import java.util.concurrent.locks.ReentrantReadWriteLock; |
19 |
< |
import java.util.concurrent.CountDownLatch; |
20 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
< |
import java.util.*; |
19 |
> |
|
20 |
> |
import junit.framework.AssertionFailedError; |
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
91 |
|
Thread.yield(); |
92 |
|
} |
93 |
|
assertTrue(t.isAlive()); |
94 |
< |
assertTrue(lock.getOwner() != t); |
94 |
> |
assertNotSame(t, lock.getOwner()); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
152 |
|
lock.writeLock().unlock(); |
153 |
|
} |
154 |
|
|
155 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
155 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
156 |
|
|
157 |
|
/** |
158 |
|
* Awaits condition using the specified AwaitMethod. |
174 |
|
java.util.Date d = new java.util.Date(); |
175 |
|
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
176 |
|
break; |
177 |
+ |
default: |
178 |
+ |
throw new AssertionError(); |
179 |
|
} |
180 |
|
} |
181 |
|
|
830 |
|
new PublicReentrantReadWriteLock(fair); |
831 |
|
try { |
832 |
|
lock.writeLock().lockInterruptibly(); |
833 |
< |
} catch (InterruptedException ie) { |
826 |
< |
threadUnexpectedException(ie); |
827 |
< |
} |
833 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
834 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
835 |
|
public void realRun() throws InterruptedException { |
836 |
|
lock.writeLock().lockInterruptibly(); |
855 |
|
lock.readLock().lockInterruptibly(); |
856 |
|
lock.readLock().unlock(); |
857 |
|
lock.writeLock().lockInterruptibly(); |
858 |
< |
} catch (InterruptedException ie) { |
853 |
< |
threadUnexpectedException(ie); |
854 |
< |
} |
858 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
859 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
860 |
|
public void realRun() throws InterruptedException { |
861 |
|
lock.readLock().lockInterruptibly(); |
881 |
|
await(c, awaitMethod); |
882 |
|
shouldThrow(); |
883 |
|
} catch (IllegalMonitorStateException success) { |
884 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
884 |
> |
} catch (InterruptedException fail) { |
885 |
> |
threadUnexpectedException(fail); |
886 |
> |
} |
887 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
888 |
|
} |
889 |
|
} |
934 |
|
assertTrue(nanosRemaining <= 0); |
935 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
936 |
|
lock.writeLock().unlock(); |
937 |
< |
} catch (InterruptedException e) { |
932 |
< |
threadUnexpectedException(e); |
933 |
< |
} |
937 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
938 |
|
} |
939 |
|
|
940 |
|
/** |
953 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
954 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
955 |
|
lock.writeLock().unlock(); |
956 |
< |
} catch (InterruptedException e) { |
953 |
< |
threadUnexpectedException(e); |
954 |
< |
} |
956 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
957 |
|
} |
958 |
|
|
959 |
|
/** |
973 |
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
974 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
975 |
|
lock.writeLock().unlock(); |
976 |
< |
} catch (InterruptedException e) { |
975 |
< |
threadUnexpectedException(e); |
976 |
< |
} |
976 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
977 |
|
} |
978 |
|
|
979 |
|
/** |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1008 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
1008 |
> |
* awaitUninterruptibly is uninterruptible |
1009 |
|
*/ |
1010 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1011 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1012 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1013 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1014 |
|
final Condition c = lock.writeLock().newCondition(); |
1015 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
1016 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
1015 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1016 |
> |
|
1017 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1018 |
|
public void realRun() { |
1019 |
+ |
// Interrupt before awaitUninterruptibly |
1020 |
|
lock.writeLock().lock(); |
1021 |
< |
locked.countDown(); |
1021 |
> |
pleaseInterrupt.countDown(); |
1022 |
> |
Thread.currentThread().interrupt(); |
1023 |
|
c.awaitUninterruptibly(); |
1024 |
|
assertTrue(Thread.interrupted()); |
1025 |
|
lock.writeLock().unlock(); |
1026 |
|
}}); |
1027 |
|
|
1028 |
< |
await(locked); |
1028 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1029 |
> |
public void realRun() { |
1030 |
> |
// Interrupt during awaitUninterruptibly |
1031 |
> |
lock.writeLock().lock(); |
1032 |
> |
pleaseInterrupt.countDown(); |
1033 |
> |
c.awaitUninterruptibly(); |
1034 |
> |
assertTrue(Thread.interrupted()); |
1035 |
> |
lock.writeLock().unlock(); |
1036 |
> |
}}); |
1037 |
> |
|
1038 |
> |
await(pleaseInterrupt); |
1039 |
|
lock.writeLock().lock(); |
1040 |
|
lock.writeLock().unlock(); |
1041 |
< |
t.interrupt(); |
1042 |
< |
long timeoutMillis = 10; |
1043 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
1041 |
> |
t2.interrupt(); |
1042 |
> |
|
1043 |
> |
assertThreadStaysAlive(t1); |
1044 |
> |
assertTrue(t2.isAlive()); |
1045 |
> |
|
1046 |
|
lock.writeLock().lock(); |
1047 |
< |
c.signal(); |
1047 |
> |
c.signalAll(); |
1048 |
|
lock.writeLock().unlock(); |
1049 |
< |
awaitTermination(t); |
1049 |
> |
|
1050 |
> |
awaitTermination(t1); |
1051 |
> |
awaitTermination(t2); |
1052 |
|
} |
1053 |
|
|
1054 |
|
/** |