1 |
package org.icesoft.util.concurrent; |
2 |
|
3 |
import static org.icesoft.util.PreCondition.checkIfIsNotNull; |
4 |
import static org.icesoft.util.PreCondition.checkIfIsNotNullAndIsNotEmpty; |
5 |
import static org.icesoft.util.concurrent.ReentrantLockUtilities.retainLocksHeldByCurrentThread; |
6 |
|
7 |
import java.util.Collections; |
8 |
import java.util.HashSet; |
9 |
import java.util.Set; |
10 |
import java.util.concurrent.TimeUnit; |
11 |
import java.util.concurrent.locks.Condition; |
12 |
import java.util.logging.Level; |
13 |
import java.util.logging.Logger; |
14 |
|
15 |
import org.icesoft.util.Named; |
16 |
|
17 |
public class Passthrough<V> |
18 |
implements Named { |
19 |
private static final Logger LOGGER = Logger.getLogger(Passthrough.class.getName()); |
20 |
|
21 |
private final Set<NamedReentrantLock> lockSet = new HashSet<NamedReentrantLock>(); |
22 |
|
23 |
private final String name; |
24 |
private final NamedReentrantLock myLock; |
25 |
private final Condition myCondition; |
26 |
private final NamedReentrantLock othersLock; |
27 |
private final NamedReentrantLock sharedLock; |
28 |
private final Condition sharedCondition; |
29 |
|
30 |
private V value; |
31 |
|
32 |
public Passthrough(final String name, final NamedReentrantLock sharedLock, final Condition sharedCondition) |
33 |
throws IllegalArgumentException, NullPointerException { |
34 |
super(); |
35 |
this.name = |
36 |
checkIfIsNotNullAndIsNotEmpty( |
37 |
name, |
38 |
"Illegal argument name: '" + name + "'. Argument cannot be null." |
39 |
); |
40 |
myLock = new NamedReentrantLock(getName() + "'s My Lock"); |
41 |
getModifiableLockSet().add(getMyLock()); |
42 |
myCondition = getMyLock().newCondition(); |
43 |
othersLock = new NamedReentrantLock(getName() + "'s Others Lock"); |
44 |
getModifiableLockSet().add(getOthersLock()); |
45 |
getModifiableLockSet().add( |
46 |
this.sharedLock = |
47 |
|
48 |
checkIfIsNotNull( |
49 |
sharedLock, |
50 |
"Illegal argument sharedLock: '" + sharedLock + "'. Argument cannot be null." |
51 |
) |
52 |
); |
53 |
this.sharedCondition = |
54 |
|
55 |
checkIfIsNotNull( |
56 |
sharedCondition, |
57 |
"Illegal argument sharedCondition: '" + sharedCondition + "'. Argument cannot be null." |
58 |
); |
59 |
} |
60 |
|
61 |
public String getName() { |
62 |
return name; |
63 |
} |
64 |
|
65 |
public void pass(final V value) |
66 |
throws InterruptedException, NullPointerException { |
67 |
|
68 |
checkIfIsNotNull(value, "Illegal argument value: '" + value + "'. Argument cannot be null."); |
69 |
|
70 |
LOGGER.info( |
71 |
"[Jack] " + Thread.currentThread().getName() + " | Trying to lock " + getMyLock().getName() + "..." |
72 |
); |
73 |
tryLockMyLock(); |
74 |
LOGGER.info( |
75 |
"[Jack] " + Thread.currentThread().getName() + " | Locked " + getMyLock().getName() + "." |
76 |
); |
77 |
try { |
78 |
this.value = value; |
79 |
|
80 |
LOGGER.info( |
81 |
"[Jack] " + Thread.currentThread().getName() + " | Trying to lock " + getSharedLock().getName() + "..." |
82 |
); |
83 |
tryLockSharedLock(); |
84 |
LOGGER.info( |
85 |
"[Jack] " + Thread.currentThread().getName() + " | Locked " + getSharedLock().getName() + "." |
86 |
); |
87 |
try { |
88 |
LOGGER.info( |
89 |
"[Jack] " + Thread.currentThread().getName() + " | Signaling all on Shared Condition..." |
90 |
); |
91 |
getSharedCondition().signalAll(); |
92 |
} finally { |
93 |
unlockSharedLock(); |
94 |
LOGGER.info( |
95 |
"[Jack] " + Thread.currentThread().getName() + " | Unlocked " + getSharedLock().getName() + "." |
96 |
); |
97 |
} |
98 |
LOGGER.info( |
99 |
"[Jack] " + Thread.currentThread().getName() + " | Awaiting on " + getName() + "'s My Condition..." |
100 |
); |
101 |
getMyCondition().await(); |
102 |
} finally { |
103 |
unlockMyLock(); |
104 |
LOGGER.info( |
105 |
"[Jack] " + Thread.currentThread().getName() + " | Unlocked " + getMyLock().getName() + "." |
106 |
); |
107 |
} |
108 |
} |
109 |
|
110 |
public V receive() |
111 |
throws InterruptedException { |
112 |
|
113 |
LOGGER.info( |
114 |
"[Jack] " + Thread.currentThread().getName() + " | Trying to lock " + getOthersLock().getName() + "..." |
115 |
); |
116 |
tryLockOthersLock(); |
117 |
LOGGER.info( |
118 |
"[Jack] " + Thread.currentThread().getName() + " | Locked " + getOthersLock().getName() + "." |
119 |
); |
120 |
try { |
121 |
|
122 |
LOGGER.info( |
123 |
"[Jack] " + Thread.currentThread().getName() + " | Trying to lock " + getMyLock().getName() + "..." |
124 |
); |
125 |
tryLockMyLock(); |
126 |
LOGGER.info( |
127 |
"[Jack] " + Thread.currentThread().getName() + " | Locked " + getMyLock().getName() + "." |
128 |
); |
129 |
try { |
130 |
V _value = this.value; |
131 |
this.value = null; |
132 |
LOGGER.info( |
133 |
"[Jack] " + Thread.currentThread().getName() + " | Signaling all on " + getName() + "'s My Condition..." |
134 |
); |
135 |
getMyCondition().signalAll(); |
136 |
return _value; |
137 |
} finally { |
138 |
unlockMyLock(); |
139 |
LOGGER.info( |
140 |
"[Jack] " + Thread.currentThread().getName() + " | Unlocked " + getMyLock().getName() + "." |
141 |
); |
142 |
} |
143 |
} finally { |
144 |
unlockOthersLock(); |
145 |
LOGGER.info( |
146 |
"[Jack] " + Thread.currentThread().getName() + " | Unlocked " + getOthersLock().getName() + "." |
147 |
); |
148 |
} |
149 |
} |
150 |
|
151 |
protected Set<NamedReentrantLock> getLockSet() { |
152 |
return Collections.unmodifiableSet(getModifiableLockSet()); |
153 |
} |
154 |
|
155 |
protected Set<NamedReentrantLock> getModifiableLockSet() { |
156 |
return lockSet; |
157 |
} |
158 |
|
159 |
protected Condition getMyCondition() { |
160 |
return myCondition; |
161 |
} |
162 |
|
163 |
protected NamedReentrantLock getMyLock() { |
164 |
return myLock; |
165 |
} |
166 |
|
167 |
protected NamedReentrantLock getOthersLock() { |
168 |
return othersLock; |
169 |
} |
170 |
|
171 |
protected Condition getSharedCondition() { |
172 |
return sharedCondition; |
173 |
} |
174 |
|
175 |
protected NamedReentrantLock getSharedLock() { |
176 |
return sharedLock; |
177 |
} |
178 |
|
179 |
protected void tryLockMyLock() |
180 |
throws InterruptedException { |
181 |
int _attemptCount = 0; |
182 |
while (!getMyLock().tryLock(300L, TimeUnit.SECONDS)) { |
183 |
_attemptCount++; |
184 |
if (LOGGER.isLoggable(Level.WARNING)) { |
185 |
LOGGER.log( |
186 |
Level.WARNING, |
187 |
"Trying to acquire My Lock failed for " + (_attemptCount * 300L) + "s. " + |
188 |
"(" + |
189 |
"Thread: '" + Thread.currentThread().getName() + "', " + |
190 |
"Locks held: '" + retainLocksHeldByCurrentThread(getLockSet()) + "'" + |
191 |
")" |
192 |
); |
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
protected void tryLockOthersLock() |
198 |
throws InterruptedException { |
199 |
int _attemptCount = 0; |
200 |
while (!getOthersLock().tryLock(300L, TimeUnit.SECONDS)) { |
201 |
_attemptCount++; |
202 |
if (LOGGER.isLoggable(Level.WARNING)) { |
203 |
LOGGER.log( |
204 |
Level.WARNING, |
205 |
"Trying to acquire Others' Lock failed for " + (_attemptCount * 300L) + "s. " + |
206 |
"(" + |
207 |
"Thread: '" + Thread.currentThread().getName() + "', " + |
208 |
"Locks held: '" + retainLocksHeldByCurrentThread(getLockSet()) + "'" + |
209 |
")" |
210 |
); |
211 |
} |
212 |
} |
213 |
} |
214 |
|
215 |
protected void tryLockSharedLock() |
216 |
throws InterruptedException { |
217 |
int _attemptCount = 0; |
218 |
while (!getSharedLock().tryLock(300L, TimeUnit.SECONDS)) { |
219 |
_attemptCount++; |
220 |
if (LOGGER.isLoggable(Level.WARNING)) { |
221 |
LOGGER.log( |
222 |
Level.WARNING, |
223 |
"Trying to acquire Shared Lock failed for " + (_attemptCount * 300L) + "s. " + |
224 |
"(" + |
225 |
"Thread: '" + Thread.currentThread().getName() + "', " + |
226 |
"Locks held: '" + retainLocksHeldByCurrentThread(getLockSet()) + "'" + |
227 |
")" |
228 |
); |
229 |
} |
230 |
} |
231 |
} |
232 |
|
233 |
protected void unlockMyLock() { |
234 |
getMyLock().unlock(); |
235 |
} |
236 |
|
237 |
protected void unlockOthersLock() { |
238 |
getOthersLock().unlock(); |
239 |
} |
240 |
|
241 |
protected void unlockSharedLock() { |
242 |
getSharedLock().unlock(); |
243 |
} |
244 |
} |