zoukankan      html  css  js  c++  java
  • Java创建线程池的三种方法

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadDemo {
        public static void main(String[] args) throws InterruptedException {
    
            ExecutorService threadPool1 = Executors.newSingleThreadExecutor();// 单个线程池
            ExecutorService threadPool2 = Executors.newFixedThreadPool(6);// 固定线程池
            ExecutorService threadPool3 = Executors.newCachedThreadPool();// 可大可小线程池(线程池大小不固定)
            for (int i = 1; i < 10; i++) {
                threadPool1.execute(() -> {
                    System.out.println("Single当前线程池名称:" + Thread.currentThread().getName());
                });
            }
            TimeUnit.SECONDS.sleep(5);
            System.out.println("===================================================");
    
            for (int i = 1; i < 10; i++) {
                threadPool2.execute(() -> {
                    System.out.println("Fixed当前线程池名称:" + Thread.currentThread().getName());
                });
            }
            TimeUnit.SECONDS.sleep(5);
            System.out.println("===================================================");
    
            for (int i = 1; i < 10; i++) {
                threadPool3.execute(() -> {
                    System.out.println("Cached当前线程池名称:" + Thread.currentThread().getName());
                });
            }
        }
    }
  • 相关阅读:
    [HAOI2009] 毛毛虫
    [NOI2015]品酒大会
    SDOI2016 生成魔咒
    [POJ2406]字符串的幂
    [SPOJ705]不同的子串
    快速幂和矩阵快速幂
    对于最近的一些日常总结by520(17.10.18)
    思维训练
    趣味性的高智商(贼有意思)的题(坑)
    C++手动开O2优化
  • 原文地址:https://www.cnblogs.com/nginxTest/p/15745346.html
Copyright © 2011-2022 走看看