9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
|
|
12 |
+ |
import java.util.ArrayList; |
13 |
|
import java.util.Arrays; |
14 |
|
import java.util.Collection; |
15 |
|
import java.util.HashSet; |
1260 |
|
sync.release(); |
1261 |
|
} |
1262 |
|
|
1263 |
+ |
/** |
1264 |
+ |
* Disabled demo test for (unfixed as of 2017-11) |
1265 |
+ |
* JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race |
1266 |
+ |
* ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck |
1267 |
+ |
*/ |
1268 |
+ |
public void XXXXtestCancelCancelRace() throws InterruptedException { |
1269 |
+ |
class Sync extends AbstractQueuedSynchronizer { |
1270 |
+ |
private static final long serialVersionUID = 1L; |
1271 |
+ |
|
1272 |
+ |
public boolean tryAcquire(int acquires) { |
1273 |
+ |
return !hasQueuedPredecessors() && compareAndSetState(0, 1); |
1274 |
+ |
} |
1275 |
+ |
|
1276 |
+ |
protected boolean tryRelease(int releases) { |
1277 |
+ |
return compareAndSetState(1, 0); |
1278 |
+ |
} |
1279 |
+ |
} |
1280 |
+ |
|
1281 |
+ |
Sync s = new Sync(); |
1282 |
+ |
s.acquire(1); // acquire to force other threads to enqueue |
1283 |
+ |
|
1284 |
+ |
// try to trigger double cancel race with two background threads |
1285 |
+ |
ArrayList<Thread> ts = new ArrayList<>(); |
1286 |
+ |
Runnable failedAcquire = () -> { |
1287 |
+ |
try { |
1288 |
+ |
s.acquireInterruptibly(1); |
1289 |
+ |
throw new AssertionError(); |
1290 |
+ |
} catch (InterruptedException expected) {} |
1291 |
+ |
}; |
1292 |
+ |
for (int i = 0; i < 2; i++) { |
1293 |
+ |
Thread t = new Thread(failedAcquire); |
1294 |
+ |
t.start(); |
1295 |
+ |
ts.add(t); |
1296 |
+ |
} |
1297 |
+ |
Thread.sleep(100); |
1298 |
+ |
for (Thread t : ts) t.interrupt(); |
1299 |
+ |
for (Thread t : ts) t.join(); |
1300 |
+ |
|
1301 |
+ |
s.release(1); |
1302 |
+ |
|
1303 |
+ |
// no one holds lock now, we should be able to acquire |
1304 |
+ |
if (!s.tryAcquire(1)) |
1305 |
+ |
throw new RuntimeException( |
1306 |
+ |
String.format( |
1307 |
+ |
"Broken: hasQueuedPredecessors=%s hasQueuedThreads=%s queueLength=%d firstQueuedThread=%s", |
1308 |
+ |
s.hasQueuedPredecessors(), |
1309 |
+ |
s.hasQueuedThreads(), |
1310 |
+ |
s.getQueueLength(), |
1311 |
+ |
s.getFirstQueuedThread())); |
1312 |
+ |
} |
1313 |
+ |
|
1314 |
|
} |