java线程执行器框架介绍(一)--ThreadPoolExecutor

本文介绍了Java多线程开发中使用执行器框架的方法,详细解释了Executors工厂类生成的ThreadPoolExecutor的不同类型及其主要执行方式,包括execute、submit、invokeAll和invokeAny方法,并通过示例展示了这些方法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在java多线程开发中,当我们需要批量执行多个任务线程时,可以使用java的执行器框架。

也就是Executors工厂类生成的各种Executor实现类。我们在这里介绍ThreadPoolExecutor。

1、由Executors工厂类生成的ThreadPoolExecutor包含如下几类:

a)newCachedThreadPool,不指定线程个数,通常用于执行线程不多的情况;

b)newFixedThreadPool,指定线程个数;

c)newSingleThreadExecutor,只有单个的执行线程;

2、ThreadPoolExecutor的执行方式,包括几种:

a)execute方法,用于执行实现Runner接口的执行线程,没有返回值;

b)submit方法,用于执行实现了Runner或Callable接口的执行线程,有返回值future;

c)invokeAll方法,用于执行多个执行线程,当多个线程均执行完后,返回结果List<Futrure<T>>;

d)invokeAny方法,用于执行多个执行线程,当其中一个线程执行完后,返回结果T;

3、实例

public class ThreadPoolExecutorMain {
    public static void main(String[] args)throws Exception {
        executeMethod();  //execute方法
        submitMethod();  //submitb方法
        invokeAllMethod(); //invokeAllMethod方法
        invokeAnyMethod(); //invokeAnyMethod方法
    }


    static void executeMethod(){
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Payer());
        executor.shutdown();
    }

    static void submitMethod() throws Exception {
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<Integer> resultFuture = executor.submit(new Payer1());
        Integer result = resultFuture.get();//等待获取结果
        System.out.println("submitMethod thread get result : "+result);
        executor.shutdown();
    }

    static void invokeAllMethod() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        List<Payer1> payerList = new ArrayList<>();
        for(int i=0;i<3;i++){
            payerList.add(new Payer1());
        }
        List<Future<Integer>> resultFutures = executor.invokeAll(payerList);
        Integer result = 0;
        for(Future<Integer> t :resultFutures){  //归总结果
            result += t.get();
        }
        System.out.println("invokeAllMethod thread get result : "+result);
        executor.shutdown();
    }

    static void invokeAnyMethod() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        List<Payer1> payerList = new ArrayList<>();
        for(int i=0;i<3;i++){
            payerList.add(new Payer1());
        }
        Integer result = executor.invokeAny(payerList);
        System.out.println("invokeAnyMethod thread get result : "+result);
        executor.shutdown();
    }

    static class Payer implements Runnable{

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+" pay 1");
        }
    }

    static class Payer1 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            int result = 1;
            System.out.println(Thread.currentThread().getName()+" payer1 return result: "+result);
            return result;
        }
    }
}

运行结果:

pool-1-thread-1 pay 1
pool-2-thread-1 payer1 return result: 1
submitMethod thread get result : 1
pool-3-thread-1 payer1 return result: 1
pool-3-thread-2 payer1 return result: 1
pool-3-thread-3 payer1 return result: 1
invokeAllMethod thread get result : 3
pool-4-thread-1 payer1 return result: 1
invokeAnyMethod thread get result : 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值