JAVA多线程——线程池

1. 固定线程池:Executors.newFixedThreadPool(5);


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author huangdh
 * @version 1.0
 * @description:
 * @date 2022-10-30 22:31
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {

        // 固定线程池
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);

        // 一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();

        // 可扩容线程池
        ExecutorService threadPool3 = Executors.newCachedThreadPool();


        // 10个顾客请求
        try {
            for (int i = 0; i < 10; i++) {
                threadPool1.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });
                /*threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                /*threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool3.shutdown();
        }
    }
}
pool-1-thread-2 正在办理业务
pool-1-thread-3 正在办理业务
pool-1-thread-1 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-5 正在办理业务
pool-1-thread-3 正在办理业务
pool-1-thread-2 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-1 正在办理业务

2. 单例线程池:Executors.newSingleThreadExecutor();


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author huangdh
 * @version 1.0
 * @description:
 * @date 2022-10-30 22:31
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {

        // 固定线程池
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);

        // 一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();

        // 可扩容线程池
        ExecutorService threadPool3 = Executors.newCachedThreadPool();


        // 10个顾客请求
        try {
            for (int i = 0; i < 10; i++) {
                /*threadPool1.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });
                /*threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool3.shutdown();
        }
    }
}
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务

3. 可扩容线程池:Executors.newCachedThreadPool();


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author huangdh
 * @version 1.0
 * @description:
 * @date 2022-10-30 22:31
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {

        // 固定线程池
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);

        // 一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();

        // 可扩容线程池
        ExecutorService threadPool3 = Executors.newCachedThreadPool();


        // 10个顾客请求
        try {
            for (int i = 0; i < 10; i++) {
                /*threadPool1.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                /*threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool3.shutdown();
        }
    }
}
pool-3-thread-1 正在办理业务
pool-3-thread-4 正在办理业务
pool-3-thread-3 正在办理业务
pool-3-thread-2 正在办理业务
pool-3-thread-5 正在办理业务
pool-3-thread-6 正在办理业务
pool-3-thread-7 正在办理业务
pool-3-thread-5 正在办理业务
pool-3-thread-2 正在办理业务
pool-3-thread-6 正在办理业务

 

posted @   BlogMemory  阅读(149)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示