线程池

线程池(重点)

池化技术
程序的运行,本质:占用系统的资源!优化资源的使用!=>池化技术线程池、连接池、内存池、对象池//I.....
池化技术:事先准备好一些资源,有人要用,就来我这里拿,用完之后还给我。

image-20230307191652647

package com.cz.pool;

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

/**
 * @author 卓亦苇
 * @version 1.0
 * 2023/3/7 19:22
 */
public class pooltest {
    public static void main(String[] args) {
        //ExecutorService executorService = Executors.newFixedThreadPool(5);//固定线程池大小
        //ExecutorService executorService = Executors.newSingleThreadExecutor();//单个线程执行
        ExecutorService executorService = Executors.newCachedThreadPool();//遇强则强,无线程上线
        for (int i = 0; i < 20; i++) {
            executorService.execute(()->{
                System.out.println(Thread.currentThread().getName()+"执行");
            });
        }
        executorService.shutdown();
    }
}
posted @ 2023-03-14 16:37  卓亦苇  阅读(13)  评论(0编辑  收藏  举报