栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【学习】线程池:ThreadPool与ThreadPoolExecutor

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【学习】线程池:ThreadPool与ThreadPoolExecutor

线程池:系列一

什么是线程池?未使用线程池调用任务方式ThreadPool线程池

ThreadPool线程池类图线程池优点线程池调用任务 ​​ThreadPoolExecutor创建线程池

​​ThreadPoolExecutor中方法​​ThreadPoolExecutor参数说明自定义线程工厂创建任务原生线程池调用任务

什么是线程池?

线程池是一种基于池化思想管理线程的工具 未使用线程池调用任务方式

首先创建Task任务类实现Runable,并输出线程名称

public class Task implements Runnable{

    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

启动线程

public class ThreadPool01 {

    public static void main(String[] args) {
        ThreadPool01.test01();
    }

    
    private static void test01(){
        // 创建任务
        Runnable task1 = new Task();
        Runnable task2 = new Task();
        Runnable task3 = new Task();
        // 创建线程
        Thread thread1 = new Thread(task1);
        Thread thread2 = new Thread(task2);
        Thread thread3 = new Thread(task3);
        // 启动线程
        thread1.start();
        thread2.start();
        thread3.start();
    }
  }

输出结果 ThreadPool线程池 ThreadPool线程池类图

ThreadPool线程池类图
​​

线程池优点

降低资源消耗:通过重复利用已创建的线程降低创建和销毁造成的消耗提高响应速度:当又任务时,任务可以不需要等到线程创建就能立即执行提高线程的可管理型:线程池可以进行统一的分配,调优和监控 线程池调用任务

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


public class ThreadPool01 {

    public static void main(String[] args) {
        ThreadPool01.test02();
    }

    
    private static void test02(){
        // 创建任务
        Runnable task1 = new Task();
        Runnable task2 = new Task();
        Runnable task3 = new Task();
        // 创建只有一个线程的线程池
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        // 提交任务
        threadPool.execute(task1);
        threadPool.execute(task2);
        threadPool.execute(task3);
        // 关闭线程池
        threadPool.shutdown();
    }

输出结果
​​ThreadPoolExecutor创建线程池 ​​ThreadPoolExecutor中方法

​​ThreadPoolExecutor参数说明

自定义线程工厂
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;


public class CustomThreadFactory implements ThreadFactory {

    // 计数器
    private final AtomicInteger i = new AtomicInteger(1);

    public Thread newThread(Runnable r) {
        // 创建线程,并指定任务
        Thread thread = new Thread(r);
        // 设置线程名称
        thread.setName("线程" + i.getAndIncrement() + "号");
        // 返回线程
        return thread;
    }
}
创建任务
public class Task implements Runnable{

    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

原生线程池调用任务
import java.util.concurrent.linkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class ThreadPool02 {


    public static void main(String[] args) {
        // 创建任务
        Runnable task1 = new Task();
        Runnable task2 = new Task();
        Runnable task3 = new Task();
        // 创建线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10,25,10, TimeUnit.SECONDS,
                new linkedBlockingQueue(),
                new CustomThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        // 提交任务
        threadPool.execute(task1);
        threadPool.execute(task2);
        threadPool.execute(task3);
        // 关闭线程池
        threadPool.shutdown();
    }
}

输出结果

参考地址:线程池视频讲解.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732370.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号