Java Executors.newCachedThreadPool Example
newCachedThreadPool 创建一个线程池,根据需要创建线程。如果有线程可用(空闲中),它将被重新使用。这个线程池中的线程是短暂的。六十秒内没有被使用的线程会被终止并从缓存中删除。这种线程池对于执行许多短暂的异步任务很有用。这些线程池被用来提高程序的性能。
newCachedThreadPool 有以下方法声明:
static ExecutorService newCachedThreadPool()
static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)Find the newCachedThreadPool example with Runnable.
public class ExecutorDemo1 {
public static void main(final String... args) throws InterruptedException {
Runnable task1 = () -> System.out.println("Executing Task-1 ...");
Runnable task2 = () -> System.out.println("Executing Task-2 ...");
ExecutorService execService = Executors.newCachedThreadPool();
execService.submit(task1);
execService.submit(task2);
execService.awaitTermination(3, TimeUnit.SECONDS);
execService.shutdownNow();
}
} Find the newCachedThreadPool example with Callable.
public class ExecutorDemo2 {
public static void main(final String... args) throws InterruptedException, ExecutionException {
Callable<String> task1 = () -> {
System.out.println("Executing Task-1");
return "Task-1 Finish.";
};
Callable<String> task2 = () -> {
System.out.println("Executing Task-2");
return "Task-2 Finish.";
};
ExecutorService execService = Executors.newCachedThreadPool();
Future<String> future1 = execService.submit(task1);
Future<String> future2 = execService.submit(task2);
String output1 = future1.get();
System.out.println(output1);
String output2 = future2.get();
System.out.println(output2);
execService.awaitTermination(3, TimeUnit.SECONDS);
execService.shutdownNow();
}
}Find the newCachedThreadPool example with given ThreadFactory.
public class ExecutorDemo3 {
public static void main(final String... args) throws InterruptedException {
Runnable task1 = () -> System.out.println("Executing Task-1 ...");
Runnable task2 = () -> System.out.println("Executing Task-2 ...");
ThreadFactory threadFactory = new MaxPriorityThreadFactory();
ExecutorService execService = Executors.newCachedThreadPool(threadFactory);
execService.submit(task1);
execService.submit(task2);
execService.awaitTermination(3, TimeUnit.SECONDS);
execService.shutdownNow();
}
}
class MaxPriorityThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setPriority(Thread.MAX_PRIORITY);
return t;
}
}也可以这样写:
public class CompletableFutureLongRunningUnitTest {
public CompletableFutureLongRunningUnitTest() throws InterruptedException, ExecutionException {
Future<String> completableFuture = calculateAsync();
String result = completableFuture.get();
System.out.println(result);
}
private Future<String> calculateAsync() {
/*
CompletableFuture<String> completableFuture = new CompletableFuture<>();
Executors.newCachedThreadPool()
.submit(() -> {
Thread.sleep(5000);
completableFuture.complete("Hello");
return null;
});
*/
// 上面的代码虽然更优雅,但是任务完成后没办法 shutdown ,下面的方法虽然多走了几步,但是可以 shutdown
CompletableFuture<String> completableFuture = new CompletableFuture<>();
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(() -> {
Thread.sleep(5000);
completableFuture.complete("Hello");
return null;
});
executor.shutdown();
return completableFuture;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
new CompletableFutureLongRunningUnitTest();
}
}参考资料:https://www.concretepage.com/java/java-executors-newcachedthreadpool
评论已关闭