1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
57 |
|
public Collection<Thread> getQueuedThreads() { |
58 |
|
return super.getQueuedThreads(); |
59 |
|
} |
59 |
– |
public PublicCondition newCondition() { |
60 |
– |
return new PublicCondition(this); |
61 |
– |
} |
62 |
– |
|
63 |
– |
static class PublicCondition extends ReentrantReadWriteLock.WriterConditionObject { |
64 |
– |
PublicCondition(PublicReentrantReadWriteLock l) { super(l); } |
65 |
– |
public Collection<Thread> getWaitingThreads() { |
66 |
– |
return super.getWaitingThreads(); |
67 |
– |
} |
68 |
– |
} |
69 |
– |
|
60 |
|
} |
61 |
|
|
62 |
|
/** |
66 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
67 |
|
assertFalse(rl.isFair()); |
68 |
|
assertFalse(rl.isWriteLocked()); |
69 |
< |
assertEquals(0, rl.getReadLocks()); |
69 |
> |
assertEquals(0, rl.getReadLockCount()); |
70 |
|
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
71 |
|
assertTrue(r2.isFair()); |
72 |
|
assertFalse(r2.isWriteLocked()); |
73 |
< |
assertEquals(0, r2.getReadLocks()); |
73 |
> |
assertEquals(0, r2.getReadLockCount()); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
81 |
|
rl.writeLock().lock(); |
82 |
|
assertTrue(rl.isWriteLocked()); |
83 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
84 |
< |
assertEquals(0, rl.getReadLocks()); |
84 |
> |
assertEquals(0, rl.getReadLockCount()); |
85 |
|
rl.writeLock().unlock(); |
86 |
|
assertFalse(rl.isWriteLocked()); |
87 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
88 |
< |
assertEquals(0, rl.getReadLocks()); |
88 |
> |
assertEquals(0, rl.getReadLockCount()); |
89 |
|
rl.readLock().lock(); |
90 |
|
assertFalse(rl.isWriteLocked()); |
91 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
92 |
< |
assertEquals(1, rl.getReadLocks()); |
92 |
> |
assertEquals(1, rl.getReadLockCount()); |
93 |
|
rl.readLock().unlock(); |
94 |
|
assertFalse(rl.isWriteLocked()); |
95 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
96 |
< |
assertEquals(0, rl.getReadLocks()); |
96 |
> |
assertEquals(0, rl.getReadLockCount()); |
97 |
|
} |
98 |
|
|
99 |
|
|
105 |
|
rl.writeLock().lock(); |
106 |
|
assertTrue(rl.isWriteLocked()); |
107 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
108 |
< |
assertEquals(0, rl.getReadLocks()); |
108 |
> |
assertEquals(0, rl.getReadLockCount()); |
109 |
|
rl.writeLock().unlock(); |
110 |
|
assertFalse(rl.isWriteLocked()); |
111 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
112 |
< |
assertEquals(0, rl.getReadLocks()); |
112 |
> |
assertEquals(0, rl.getReadLockCount()); |
113 |
|
rl.readLock().lock(); |
114 |
|
assertFalse(rl.isWriteLocked()); |
115 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
116 |
< |
assertEquals(1, rl.getReadLocks()); |
116 |
> |
assertEquals(1, rl.getReadLockCount()); |
117 |
|
rl.readLock().unlock(); |
118 |
|
assertFalse(rl.isWriteLocked()); |
119 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
120 |
< |
assertEquals(0, rl.getReadLocks()); |
120 |
> |
assertEquals(0, rl.getReadLockCount()); |
121 |
|
} |
122 |
|
|
123 |
|
/** |
910 |
|
*/ |
911 |
|
public void testHasWaiters() { |
912 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
913 |
< |
final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition()); |
913 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.writeLock().newCondition()); |
914 |
|
Thread t = new Thread(new Runnable() { |
915 |
|
public void run() { |
916 |
|
try { |
952 |
|
*/ |
953 |
|
public void testGetWaitQueueLength() { |
954 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
955 |
< |
final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition()); |
955 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.writeLock().newCondition()); |
956 |
|
Thread t1 = new Thread(new Runnable() { |
957 |
|
public void run() { |
958 |
|
try { |
1007 |
|
unexpectedException(); |
1008 |
|
} |
1009 |
|
} |
1020 |
– |
|
1021 |
– |
/** |
1022 |
– |
* getWaitingThreads returns only and all waiting threads |
1023 |
– |
*/ |
1024 |
– |
public void testGetWaitingThreads() { |
1025 |
– |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1026 |
– |
final PublicReentrantReadWriteLock.PublicCondition c = (PublicReentrantReadWriteLock.PublicCondition)lock.newCondition(); |
1027 |
– |
Thread t1 = new Thread(new Runnable() { |
1028 |
– |
public void run() { |
1029 |
– |
try { |
1030 |
– |
lock.writeLock().lock(); |
1031 |
– |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
1032 |
– |
c.await(); |
1033 |
– |
lock.writeLock().unlock(); |
1034 |
– |
} |
1035 |
– |
catch(InterruptedException e) { |
1036 |
– |
threadUnexpectedException(); |
1037 |
– |
} |
1038 |
– |
} |
1039 |
– |
}); |
1040 |
– |
|
1041 |
– |
Thread t2 = new Thread(new Runnable() { |
1042 |
– |
public void run() { |
1043 |
– |
try { |
1044 |
– |
lock.writeLock().lock(); |
1045 |
– |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
1046 |
– |
c.await(); |
1047 |
– |
lock.writeLock().unlock(); |
1048 |
– |
} |
1049 |
– |
catch(InterruptedException e) { |
1050 |
– |
threadUnexpectedException(); |
1051 |
– |
} |
1052 |
– |
} |
1053 |
– |
}); |
1054 |
– |
|
1055 |
– |
try { |
1056 |
– |
lock.writeLock().lock(); |
1057 |
– |
assertTrue(c.getWaitingThreads().isEmpty()); |
1058 |
– |
lock.writeLock().unlock(); |
1059 |
– |
t1.start(); |
1060 |
– |
Thread.sleep(SHORT_DELAY_MS); |
1061 |
– |
t2.start(); |
1062 |
– |
Thread.sleep(SHORT_DELAY_MS); |
1063 |
– |
lock.writeLock().lock(); |
1064 |
– |
assertTrue(c.hasWaiters()); |
1065 |
– |
assertTrue(c.getWaitingThreads().contains(t1)); |
1066 |
– |
assertTrue(c.getWaitingThreads().contains(t2)); |
1067 |
– |
c.signalAll(); |
1068 |
– |
lock.writeLock().unlock(); |
1069 |
– |
Thread.sleep(SHORT_DELAY_MS); |
1070 |
– |
lock.writeLock().lock(); |
1071 |
– |
assertFalse(c.hasWaiters()); |
1072 |
– |
assertTrue(c.getWaitingThreads().isEmpty()); |
1073 |
– |
lock.writeLock().unlock(); |
1074 |
– |
t1.join(SHORT_DELAY_MS); |
1075 |
– |
t2.join(SHORT_DELAY_MS); |
1076 |
– |
assertFalse(t1.isAlive()); |
1077 |
– |
assertFalse(t2.isAlive()); |
1078 |
– |
} |
1079 |
– |
catch (Exception ex) { |
1080 |
– |
unexpectedException(); |
1081 |
– |
} |
1082 |
– |
} |
1083 |
– |
|
1010 |
|
} |