例1
public class ThreadPoolDemo01 {
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 1000; j++) {
executorService.submit(() -> {
synchronized (ThreadPoolDemo01.class) {
count++;
}
});
}
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
System.out.println("Final count: " + count);
}
}
例2
public class ThreadPoolDemo02 {
private final LongAdder count = new LongAdder();
public void increment() {
count.increment();
}
public long getCount() {
return count.longValue();
}
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
ThreadPoolDemo02 demo02 = new ThreadPoolDemo02();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 1000; j++) {
executorService.submit(demo02::increment);
}
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
System.out.println("Final count: " + demo02.getCount());
}
}
例3
public class ThreadPoolDemo03 {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
ExecutorService executorService = new ThreadPoolExecutor(
10, 10, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>());
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 1000; j++) {
executorService.submit(counter::increment);
}
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
System.out.println("Final count: " + counter.getCount());
}
}
public class Counter {
private final LongAdder count = new LongAdder();
public void increment() {
count.increment();
}
public long getCount() {
return count.longValue();
}
}
例4
public class ThreadPoolDemo04 {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
ExecutorService executorService = new ThreadPoolExecutor(
10, 10, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>());
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 1000; j++) {
executorService.submit(counter::increment);
}
}
executorService.shutdown();
try {
executorService.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Final count: " + counter.getCount());
}
}
public class Counter {
private final LongAdder count = new LongAdder();
public void increment() {
count.increment();
}
public long getCount() {
return count.longValue();
}
}
例5
public class ThreadPoolDemo05 {
private static final int THREAD_POOL_SIZE = 10;
private static final int TASK_COUNT = 10000;
private static final int AWAIT_TERMINATION_TIME = 1;
private static final TimeUnit AWAIT_TERMINATION_TIME_UNIT = TimeUnit.MINUTES;
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
ExecutorService executorService = new ThreadPoolExecutor(
THREAD_POOL_SIZE, THREAD_POOL_SIZE, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new NamedThreadFactory("ThreadPoolDemo"));
for (int i = 0; i < THREAD_POOL_SIZE; i++) {
for (int j = 0; j < TASK_COUNT; j++) {
executorService.submit(counter::increment);
}
}
executorService.shutdown();
try {
executorService.awaitTermination(AWAIT_TERMINATION_TIME, AWAIT_TERMINATION_TIME_UNIT);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Final count: " + counter.getCount());
}
}
public class Counter {
private final LongAdder count = new LongAdder();
public void increment() {
count.increment();
}
public long getCount() {
return count.longValue();
}
}
public class NamedThreadFactory implements ThreadFactory {
private final String namePrefix;
private final AtomicInteger threadNumber = new AtomicInteger(1);
public NamedThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
}
@Override
public Thread newThread(Runnable r) {
return new Thread(r, namePrefix + "-thread-" + threadNumber.getAndIncrement());
}
}
例6
public class ThreadPoolDemo06 {
private static final int THREAD_POOL_SIZE = 10;
private static final int TASK_COUNT = 1000;
private static final int AWAIT_TERMINATION_TIME = 1;
private static final TimeUnit AWAIT_TERMINATION_TIME_UNIT = TimeUnit.MINUTES;
public static void main(String[] args) throws InterruptedException, ExecutionException {
Counter01 counter = new Counter01();
ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE, new NamedThreadFactory("ThreadPoolDemo"));
List<CompletableFuture<Void>> futureList = new ArrayList<>();
for (int i = 0; i < THREAD_POOL_SIZE; i++) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
for (int j = 0; j < TASK_COUNT; j++) {
counter.increment();
}
}, executorService);
futureList.add(future);
}
CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]))
.exceptionally(ex -> {
ex.printStackTrace();
return null;
}).get();
executorService.shutdown();
try {
executorService.awaitTermination(AWAIT_TERMINATION_TIME, AWAIT_TERMINATION_TIME_UNIT);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Final count: " + counter.getCount());
}
}
public class Counter01 {
private final ThreadLocal<LongAdder> count = ThreadLocal.withInitial(LongAdder::new);
public void increment() {
count.get().increment();
}
public long getCount() {
return count.get().longValue();
}
}
public class NamedThreadFactory implements ThreadFactory {
private final String namePrefix;
private final AtomicInteger threadNumber = new AtomicInteger(1);
public NamedThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
}
@Override
public Thread newThread(Runnable r) {
return new Thread(r, namePrefix + "-thread-" + threadNumber.getAndIncrement());
}
}