12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.HashSet; |
15 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
18 |
|
|
1257 |
|
sync.release(); |
1258 |
|
} |
1259 |
|
|
1260 |
+ |
/** |
1261 |
+ |
* Tests scenario for |
1262 |
+ |
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1263 |
+ |
*/ |
1264 |
+ |
public void testInterruptedFailingAcquire() throws InterruptedException { |
1265 |
+ |
final RuntimeException ex = new RuntimeException(); |
1266 |
+ |
|
1267 |
+ |
// A synchronizer only offering a choice of failure modes |
1268 |
+ |
class Sync extends AbstractQueuedLongSynchronizer { |
1269 |
+ |
boolean pleaseThrow; |
1270 |
+ |
@Override protected boolean tryAcquire(long ignored) { |
1271 |
+ |
if (pleaseThrow) throw ex; |
1272 |
+ |
return false; |
1273 |
+ |
} |
1274 |
+ |
@Override protected long tryAcquireShared(long ignored) { |
1275 |
+ |
if (pleaseThrow) throw ex; |
1276 |
+ |
return -1; |
1277 |
+ |
} |
1278 |
+ |
@Override protected boolean tryRelease(long ignored) { |
1279 |
+ |
return true; |
1280 |
+ |
} |
1281 |
+ |
@Override protected boolean tryReleaseShared(long ignored) { |
1282 |
+ |
return true; |
1283 |
+ |
} |
1284 |
+ |
} |
1285 |
+ |
|
1286 |
+ |
final Sync s = new Sync(); |
1287 |
+ |
|
1288 |
+ |
final Thread thread = newStartedThread(new CheckedRunnable() { |
1289 |
+ |
public void realRun() { |
1290 |
+ |
try { |
1291 |
+ |
if (ThreadLocalRandom.current().nextBoolean()) |
1292 |
+ |
s.acquire(1); |
1293 |
+ |
else |
1294 |
+ |
s.acquireShared(1); |
1295 |
+ |
shouldThrow(); |
1296 |
+ |
} catch (Throwable t) { |
1297 |
+ |
assertSame(ex, t); |
1298 |
+ |
assertTrue(Thread.interrupted()); |
1299 |
+ |
} |
1300 |
+ |
}}); |
1301 |
+ |
waitForThreadToEnterWaitState(thread); |
1302 |
+ |
assertSame(thread, s.getFirstQueuedThread()); |
1303 |
+ |
assertTrue(s.hasQueuedPredecessors()); |
1304 |
+ |
assertTrue(s.hasQueuedThreads()); |
1305 |
+ |
assertEquals(1, s.getQueueLength()); |
1306 |
+ |
|
1307 |
+ |
s.pleaseThrow = true; |
1308 |
+ |
thread.interrupt(); |
1309 |
+ |
s.release(1); |
1310 |
+ |
awaitTermination(thread); |
1311 |
+ |
} |
1312 |
+ |
|
1313 |
|
} |