-
How the thread is implemented
-
Use of thread pool
The main differences between newCachedThreadPool and SingleThreadExecutor are corePoolSize, maximumPoolSize, and SynchronousQueue for blocking queues. When the blocking queue used is SynchronousQueue, the size of corePoolSize has expired. NewCachedThreadPool will only reuse idle and available threads. When there are no available threads, Will continue to create new threads, so personally is not very recommended.
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
Sample code:
public class CachedThreadPoolTest { // 线程数 private static final int threads = 10; // 用于计数线程是否执行完成 CountDownLatch countDownLatch = new CountDownLatch(threads); /** * newCachedThreadPool execute * * @throws ExecutionException * @throws InterruptedException */ @Test public void test1() throws InterruptedException { System.out.println("---- start ----"); ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < threads; i++) { cachedThreadPool.execute(() -> { try { System.out.println(Thread.currentThread().getName()); } catch (Exception e) { System.out.println(e); } finally { countDownLatch.countDown(); } }); } countDownLatch.await(); System.out.println("---- end ----"); } /** * newCachedThreadPool submit submit */ @Test public void test2() throws InterruptedException { System.out.println("---- start ----"); ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < threads; i++) { // Callable 带返回值 cachedThreadPool.submit(new Thread(new Runnable() { @Override public void run() { try { System.out.println(Thread.currentThread().getName()); } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } } })); } countDownLatch.await(); System.out.println("---- end ----"); } /** * newCachedThreadPool submit Callable * * @throws ExecutionException * @throws InterruptedException */ @Test public void test3() throws ExecutionException, InterruptedException { System.out.println("---- start ----"); ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < threads; i++) { // Runnable 带返回值 FutureTask<?> futureTask = new FutureTask<>(new Callable<String>() { /** * call * @return currentThreadName */ @Override public String call() { return Thread.currentThread().getName(); } }); cachedThreadPool.submit(new Thread(futureTask)); System.out.println(futureTask.get()); } System.out.println("---- end ----"); } }