java 多线程(ThreadPoolExecutor)

ThreadPoolExecutor是常用的java线程池实现,之前的项目中也一直有用到,本文记录两个基本的例子和参数设置。

例子1:

复制代码
public class Thread1 {
    public void ThreadOperation(){
        BlockingQueue queue = new LinkedBlockingQueue();
        ThreadPoolExecutor executer 
            = new ThreadPoolExecutor(3,6,1,TimeUnit.DAYS,queue);
        
        for(int i = 0; i < 10; i++){
            executer.execute(new Runnable(){

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("execute:");
                }
            });
           
        executer.shutdown();
    }
}
复制代码

例子2:

复制代码
public class Thread2 {
    
    class MyFactory implements ThreadFactory{

        @Override
        public Thread newThread(Runnable r) {
            // TODO Auto-generated method stub
            System.out.println("newThread");
            Thread t = new Thread(r);
            t.setName("Fredric");
            return t;
        }    
    }
    
    public void ThreadOperation(){
        BlockingQueue queue = new LinkedBlockingQueue(4);//特地限制了缓存队列的大小
        ThreadPoolExecutor executor 
            = new ThreadPoolExecutor(3,//初始线程数
                    6,//当缓存满时创建的最大线程数
                    100,//多余线程等待任务的时间
                    TimeUnit.MILLISECONDS,//时间的单位
                    queue,
                    new MyFactory());
        
        for(int i = 0; i < 10; i++){
            executor.execute(new Runnable(){

                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("executer"+
                            Thread.currentThread().getName());
                }    
            });
        }
        
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        
        for(int i = 0; i < 5; i++){
            executor.execute(new Runnable(){

                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("executer "+" next "+
                            Thread.currentThread().getName());
                }    
            });
        }        
        executor.shutdown();        
    }
}
复制代码

第二个例子证明参数的使用,其打印如下:

newThread
newThread
newThread //系统启动时的核心线程 3个
newThread
newThread
newThread //当缓存队列满时,增加线程至 10个
executerFredric
executerFredric
executerFredric
executerFredric
executerFredric
executerFredric
executerFredric
executerFredric
executerFredric
executerFredric 
newThread
newThread
newThread //超时后,新增的3个线程被销毁,因此被重新创建
executer next Fredric
executer next Fredric
executer next Fredric
executer next Fredric
executer next Fredric

posted @   Fredric_2013  阅读(335)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示