当前位置: 首页 > article >正文

【Java】实现一个自己的线程池

上文中我们讲了线程池的简单使用,这里我们来讲一下如何简单实现一个自己的线程池

本文实现这个线程池所达到的效果是:用户给出线程数目,程序根据用户给出的数创建固定数目的线程

1、框架

首先写定义一个线程池类

class MyThreadPool{
    
}

public class demo3 {
    public static void main(String[] args) throws InterruptedException {
        
    }
}

然后往MyThreadPool类中填充内容

 2、成员变量

首先定义成员变量

由于线程池中需要一个阻塞队列来存放任务,于是我们便先实现一个LinkedBlockingDeque阻塞队列。成员属性设置为私有

private BlockingDeque<Runnable> queue = new LinkedBlockingDeque<Runnable>();

什么是阻塞队列?

 3、submit()方法

线程池类型中还需要书写一个submit()方法用以添加任务

public void submit(Runnable runnable) throws InterruptedException {
    queue.put(runnable);
}

 4、构造方法

在MyThreadPool构造方法中,我们需要创建用户给定数目的线程,并用这些线程去执行队列中的任务

public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            int id = i;
            Thread thread = new Thread(()->{
                Runnable runnable = null;
                try {
                    runnable = queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                runnable.run();
            });
            thread.start();
        }
}

5、完整代码

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class MyThreadPool{
    private BlockingDeque<Runnable> queue = new LinkedBlockingDeque<Runnable>();

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            int id = i;
            Thread thread = new Thread(()->{
                Runnable runnable = null;
                try {
                    runnable = queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                runnable.run();
            });
            thread.start();
        }
    }
}

public class demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool threadPool = new MyThreadPool(10);
        for (int i = 0; i < 10; i++) {
            int id = i;
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("任务" + id);
                }
            });
        }
    }
}

运行结果

 


http://www.kler.cn/news/148173.html

相关文章:

  • 基于阶梯碳交易的含P2G-CCS耦合和燃气掺氢的虚拟电厂优化调度matlab程序
  • Slf4j使用Logback时,Logback如何初始化
  • 大语言模型损失函数详解
  • 【论文阅读笔记】Smil: Multimodal learning with severely missing modality
  • paddleocr笔记
  • cocos2dx ​​Animate3D(二)
  • 【TiDB】TiDB离线方式部署
  • Ubuntu18.04安装A-Loam保姆级教程
  • BUUCTF [MRCTF2020]Ez_bypass 1
  • npm管理发布包-创建与发布
  • python:range函数的使用
  • 蓝桥杯-01简介
  • 「江鸟中原」有关HarmonyOS-ArkTS的Http通信请求
  • 获得文件MD5——校验完整性 window 和 Linux下操作
  • 2023亚太地区数学建模C题思路分析+模型+代码+论文
  • oracle 表树形结构查询递归查询
  • 学习知识回顾随笔(远程连接MySQL|远程访问Django|HTTP协议|Web框架)
  • assert
  • Nevron Vision for .NET 2023.1 Crack
  • vue脚手架的基础搭建过程
  • Android frameworks 开发总结之九(Settings)
  • electron入门(一)环境搭建,实现样例
  • Rust UI开发(四):iced中如何添加菜单栏(串口调试助手)
  • 【React】打包优化-配置CDN
  • 002、ArkTS
  • 计算机基础知识59
  • python循环
  • ESP32-Web-Server编程- JS 基础 4
  • 从赛车到服务台:IT团队可以从F1赛车中学到什么?
  • 了解JSX