37 import static java.util.concurrent.TimeUnit.NANOSECONDS; |
37 import static java.util.concurrent.TimeUnit.NANOSECONDS; |
38 |
38 |
39 import java.util.Arrays; |
39 import java.util.Arrays; |
40 import java.util.Collection; |
40 import java.util.Collection; |
41 import java.util.HashSet; |
41 import java.util.HashSet; |
|
42 import java.util.concurrent.ThreadLocalRandom; |
42 import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
43 import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
43 import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
44 import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
44 |
45 |
45 import junit.framework.AssertionFailedError; |
46 import junit.framework.AssertionFailedError; |
46 import junit.framework.Test; |
47 import junit.framework.Test; |
1281 assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1282 assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1282 assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1283 assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1283 sync.release(); |
1284 sync.release(); |
1284 } |
1285 } |
1285 |
1286 |
|
1287 /** |
|
1288 * Tests scenario for |
|
1289 * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
|
1290 */ |
|
1291 public void testInterruptedFailingAcquire() throws InterruptedException { |
|
1292 final RuntimeException ex = new RuntimeException(); |
|
1293 |
|
1294 // A synchronizer only offering a choice of failure modes |
|
1295 class Sync extends AbstractQueuedLongSynchronizer { |
|
1296 boolean pleaseThrow; |
|
1297 @Override protected boolean tryAcquire(long ignored) { |
|
1298 if (pleaseThrow) throw ex; |
|
1299 return false; |
|
1300 } |
|
1301 @Override protected long tryAcquireShared(long ignored) { |
|
1302 if (pleaseThrow) throw ex; |
|
1303 return -1; |
|
1304 } |
|
1305 @Override protected boolean tryRelease(long ignored) { |
|
1306 return true; |
|
1307 } |
|
1308 @Override protected boolean tryReleaseShared(long ignored) { |
|
1309 return true; |
|
1310 } |
|
1311 } |
|
1312 |
|
1313 final Sync s = new Sync(); |
|
1314 |
|
1315 final Thread thread = newStartedThread(new CheckedRunnable() { |
|
1316 public void realRun() { |
|
1317 try { |
|
1318 if (ThreadLocalRandom.current().nextBoolean()) |
|
1319 s.acquire(1); |
|
1320 else |
|
1321 s.acquireShared(1); |
|
1322 shouldThrow(); |
|
1323 } catch (Throwable t) { |
|
1324 assertSame(ex, t); |
|
1325 assertTrue(Thread.interrupted()); |
|
1326 } |
|
1327 }}); |
|
1328 waitForThreadToEnterWaitState(thread); |
|
1329 assertSame(thread, s.getFirstQueuedThread()); |
|
1330 assertTrue(s.hasQueuedPredecessors()); |
|
1331 assertTrue(s.hasQueuedThreads()); |
|
1332 assertEquals(1, s.getQueueLength()); |
|
1333 |
|
1334 s.pleaseThrow = true; |
|
1335 thread.interrupt(); |
|
1336 s.release(1); |
|
1337 awaitTermination(thread); |
|
1338 } |
|
1339 |
1286 } |
1340 } |