-
Bug
-
Resolution: Fixed
-
P3
-
6
-
b08
-
generic, x86
-
generic, linux
-
Not verified
ThreadPoolExecutor.setCorePoolSize goes to a lot of trouble to
kill off excess idling threads, but the code is not correct.
--------------
import java.util.*;
import java.util.concurrent.*;
public class Bug4 {
static void report(String label, ThreadPoolExecutor tpe) {
System.out.printf("%10s: active=%d, submitted=%d, completed=%d%n",
label,
Thread.activeCount() - 1,
tpe.getTaskCount(),
tpe.getCompletedTaskCount());
}
public static void main(String[] args) throws Throwable {
final int count = 8;
final CyclicBarrier barrier = new CyclicBarrier(count + 1);
ThreadPoolExecutor tpe =
new ThreadPoolExecutor(10, 30, 1L, TimeUnit.HOURS,
new LinkedBlockingQueue<Runnable>());
report("newborn", tpe);
for (int i = 0; i < count; i++)
tpe.execute(new Runnable() {
public void run() {
try { barrier.await(); barrier.await(); }
catch (Throwable t) { t.printStackTrace(); }
}});
barrier.await();
report("started", tpe);
barrier.await();
Thread.sleep(1000);
report("idling", tpe);
tpe.setCorePoolSize(count/2);
Thread.sleep(1000);
report("shrunk", tpe);
System.out.printf("Shutting down...%n");
tpe.shutdown();
tpe.awaitTermination(1L, TimeUnit.HOURS);
report("terminated", tpe);
}
}
--------------
$ jver 6 jr Bug4
==> javac -source 1.6 -Xlint:all Bug4.java
==> java -esa -ea Bug4
newborn: active=0, submitted=0, completed=0
started: active=8, submitted=8, completed=0
idling: active=8, submitted=8, completed=8
shrunk: active=8, submitted=8, completed=8
Shutting down...
terminated: active=0, submitted=8, completed=8
kill off excess idling threads, but the code is not correct.
--------------
import java.util.*;
import java.util.concurrent.*;
public class Bug4 {
static void report(String label, ThreadPoolExecutor tpe) {
System.out.printf("%10s: active=%d, submitted=%d, completed=%d%n",
label,
Thread.activeCount() - 1,
tpe.getTaskCount(),
tpe.getCompletedTaskCount());
}
public static void main(String[] args) throws Throwable {
final int count = 8;
final CyclicBarrier barrier = new CyclicBarrier(count + 1);
ThreadPoolExecutor tpe =
new ThreadPoolExecutor(10, 30, 1L, TimeUnit.HOURS,
new LinkedBlockingQueue<Runnable>());
report("newborn", tpe);
for (int i = 0; i < count; i++)
tpe.execute(new Runnable() {
public void run() {
try { barrier.await(); barrier.await(); }
catch (Throwable t) { t.printStackTrace(); }
}});
barrier.await();
report("started", tpe);
barrier.await();
Thread.sleep(1000);
report("idling", tpe);
tpe.setCorePoolSize(count/2);
Thread.sleep(1000);
report("shrunk", tpe);
System.out.printf("Shutting down...%n");
tpe.shutdown();
tpe.awaitTermination(1L, TimeUnit.HOURS);
report("terminated", tpe);
}
}
--------------
$ jver 6 jr Bug4
==> javac -source 1.6 -Xlint:all Bug4.java
==> java -esa -ea Bug4
newborn: active=0, submitted=0, completed=0
started: active=8, submitted=8, completed=0
idling: active=8, submitted=8, completed=8
shrunk: active=8, submitted=8, completed=8
Shutting down...
terminated: active=0, submitted=8, completed=8
- duplicates
-
JDK-7028444 ThreadPoolExecutor fails to halt extra threads if reconfigured to a smaller pool
- Closed
- relates to
-
JDK-6576792 ThreadPoolExecutor methods leak interrupts when run in pool threads
- Resolved