线程池:ThreadPoolExecutor

1.线程池ThreadPoolExecutor引入
思路:创建好多个线程,放入线程池中,使用时直接获取引用,不使用时放回池中。可以避免频繁创建销毁、实现重复利用

2.JDK1.5起,提供了内置线程池

3.线程池的好处:
提高响应速度(减少了创建新线程的时间)
降低资源消耗(重复利用线程池中线程,不需要每次都创建)
提高线程的可管理性:避免线程无限制创建、从而销耗系统资源,降低系统稳定性,甚至内存溢出或者CPU耗尽

4.线程池的应用场合:
需要大量线程,并且完成任务的时间短
对性能要求苛刻
接受突发性的大量请求

使用线程池实现大量的Runnable接口

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class TestThreadPool1 {
    public static void main(String[] args) {
        //1.创建一个线程池
        
        //线程池中只有一个线程Single
        //ExecutorService pool = Executors.newSingleThreadExecutor();
        //线程池中有固定数量的线程Fixed
        //ExecutorService pool = Executors.newFixedThreadPool(10);
        //线程池中线程的数量可以动态的变化(增长减少) Cached
        ExecutorService pool = Executors.newCachedThreadPool();
        //Scheduled 定时任务 用来执行大量的定时任务(几点执行、间隔执行)
        //ExecutorService pool4 = Executors.newScheduledThreadPool(10);
        //ExecutorService pool5 = new ThreadPoolExecutor();

        //2.使用线程池执行大量的Runnable命令
        for (int i = 0; i < 20; i++) {
            Runnable command = new MyRunnable(i);
            //new Thread(runnable).start();
            pool.execute(command);
        }
        //3.关闭线程池
        pool.shutdown();
    }
}
class MyRunnable implements Runnable{
    private int i;
    public MyRunnable(int i) {
        this.i = i;
    }
    @Override
    public void run() {
        System.out.println("任务"+i+"开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("任务"+i+"结束");
    }
}

使用线程池实现大量的Callable接口

import jdk.nashorn.internal.codegen.CompilerConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
public class TestThreadPool2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
     
        //1.创建一个线程池
        
        //线程池中只有一个线程Single
        //ExecutorService pool = Executors.newSingleThreadExecutor();
        //线程池中有固定数量的线程Fixed
        //ExecutorService pool = Executors.newFixedThreadPool(10);
        //线程池中线程的数量可以动态的变化(增长减少) Cached
        ExecutorService pool = Executors.newCachedThreadPool();
        
        //2.使用线程池执行大量的Callable任务
        List<Future> futureList = new ArrayList<Future>();
        for (int i = 0; i <20 ; i++) {
            final int n = i;
            Callable<Integer> task = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    System.out.println("线程开始:"+n);
                    Thread.sleep(3000);
                    int result = new Random().nextInt(10);
                    System.out.println("线程结束:"+n);
                    return result;
                }
            };
            Future<Integer> future = pool.submit(task);
            futureList.add(future);
            //int result = future.get();//????
            //System.out.println(result);
        }
        for (int i = 0; i < 20; i++) {
           Future future=  futureList.get(i);
            System.out.println(future.get());
        }
        //3.关闭线程池
        pool.shutdown();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值