test/java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
--- 1,7 ----
/*
! * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*** 74,83 ****
--- 74,85 ----
/** Poor man's bounded buffer. */
final AtomicLong approximateCount = new AtomicLong(0L);
abstract class CheckedThread extends Thread {
+ protected volatile boolean stopRequest = false;
+
CheckedThread(String name) {
super(name);
setDaemon(true);
start();
}
*** 85,157 ****
protected boolean quittingTime() {
return System.nanoTime() - quittingTimeNanos > 0;
}
/** Polls occasionally for quitting time. */
protected boolean quittingTime(long i) {
! return (i % 1024) == 0 && quittingTime();
}
abstract protected void realRun();
public void run() {
try { realRun(); } catch (Throwable t) { unexpected(t); }
}
}
! Thread offerer = new CheckedThread("offerer") {
protected void realRun() {
final long chunkSize = getRandom().nextInt(maxChunkSize) + 2;
long c = 0;
for (long i = 0; ! quittingTime(i); i++) {
if (q.offer(Long.valueOf(c))) {
if ((++c % chunkSize) == 0) {
approximateCount.getAndAdd(chunkSize);
while (approximateCount.get() > maxQueueSize)
! Thread.yield();
}
} else {
! Thread.yield();
}}}};
! Thread remover = new CheckedThread("remover") {
protected void realRun() {
final long chunkSize = getRandom().nextInt(maxChunkSize) + 2;
long c = 0;
for (long i = 0; ! quittingTime(i); i++) {
if (q.remove(Long.valueOf(c))) {
if ((++c % chunkSize) == 0) {
approximateCount.getAndAdd(-chunkSize);
}
} else {
! Thread.yield();
}
}
q.clear();
approximateCount.set(0); // Releases waiting offerer thread
}};
! Thread scanner = new CheckedThread("scanner") {
protected void realRun() {
final Random rnd = getRandom();
while (! quittingTime()) {
switch (rnd.nextInt(3)) {
case 0: checkNotContainsNull(q); break;
case 1: q.size(); break;
case 2: checkNotContainsNull
(Arrays.asList(q.toArray(new Long[0])));
break;
}
! Thread.yield();
}}};
! for (Thread thread : new Thread[] { offerer, remover, scanner }) {
thread.join(timeoutMillis + testDurationMillis);
if (thread.isAlive()) {
System.err.printf("Hung thread: %s%n", thread.getName());
failed++;
for (StackTraceElement e : thread.getStackTrace())
System.err.println(e);
// Kludge alert
! thread.stop();
thread.join(timeoutMillis);
}
}
}
--- 87,167 ----
protected boolean quittingTime() {
return System.nanoTime() - quittingTimeNanos > 0;
}
/** Polls occasionally for quitting time. */
protected boolean quittingTime(long i) {
! return stopRequest || quittingTime() && (i % 1024 == 0 || i < 1024);
! }
! protected void giveupCPU(){
! try {
! Thread.sleep(0L);
! } catch (InterruptedException ignore) {}
}
abstract protected void realRun();
public void run() {
try { realRun(); } catch (Throwable t) { unexpected(t); }
}
+ public void stopThread() {
+ stopRequest = true;
+ }
}
! CheckedThread offerer = new CheckedThread("offerer") {
protected void realRun() {
final long chunkSize = getRandom().nextInt(maxChunkSize) + 2;
long c = 0;
for (long i = 0; ! quittingTime(i); i++) {
if (q.offer(Long.valueOf(c))) {
if ((++c % chunkSize) == 0) {
approximateCount.getAndAdd(chunkSize);
while (approximateCount.get() > maxQueueSize)
! giveupCPU();
}
} else {
! giveupCPU();
}}}};
! CheckedThread remover = new CheckedThread("remover") {
protected void realRun() {
final long chunkSize = getRandom().nextInt(maxChunkSize) + 2;
long c = 0;
for (long i = 0; ! quittingTime(i); i++) {
if (q.remove(Long.valueOf(c))) {
if ((++c % chunkSize) == 0) {
approximateCount.getAndAdd(-chunkSize);
}
} else {
! giveupCPU();
}
}
q.clear();
approximateCount.set(0); // Releases waiting offerer thread
}};
! CheckedThread scanner = new CheckedThread("scanner") {
protected void realRun() {
final Random rnd = getRandom();
while (! quittingTime()) {
switch (rnd.nextInt(3)) {
case 0: checkNotContainsNull(q); break;
case 1: q.size(); break;
case 2: checkNotContainsNull
(Arrays.asList(q.toArray(new Long[0])));
break;
}
! giveupCPU();
}}};
! for (CheckedThread thread : new CheckedThread[] { offerer, remover, scanner }) {
thread.join(timeoutMillis + testDurationMillis);
if (thread.isAlive()) {
System.err.printf("Hung thread: %s%n", thread.getName());
failed++;
for (StackTraceElement e : thread.getStackTrace())
System.err.println(e);
// Kludge alert
! thread.stopThread();
thread.join(timeoutMillis);
}
}
}