【线程池处理任务】Runnable任务和Callable任务

线程池处理任务

ExecutorService的常用方法

方法名称说明
void execute(Runnable command)执行 Runnable 任务
Future< T > submit(Callable< T > task)执行 Callable 任务,返回未来任务对象,用于获取线程返回的结果
void shutdown()等全部任务执行完毕后,再关闭线程池!
List< Runnable > shutdownNow()立刻关闭线程池,停止正在执行的任务,并返回队列中未执行的任务
1.线程池处理Runnable任务
public class Demo {
    public static void main(String[] args) {
        //创建线程池对象
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                5,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy() //默认拒绝策略:任务,抛出异常
        );
        //public void execute(Runnable command) 执行Runnable任务
        pool.execute(new MyRunnable()); //核心
        pool.execute(new MyRunnable()); //核心
        pool.execute(new MyRunnable()); //核心

        pool.execute(new MyRunnable()); //任务队列
        pool.execute(new MyRunnable()); //任务队列
        pool.execute(new MyRunnable()); //任务队列
        pool.execute(new MyRunnable()); //任务队列

        pool.execute(new MyRunnable()); //临时线程
        pool.execute(new MyRunnable()); //临时线程

        //pool.execute(new MyRunnable()); //被拒绝了

        //public void shutdown(); 等全部任务执行完毕后,关闭线程池
        pool.shutdown();
    }
}
class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "执行了");
    }
}

在这里插入图片描述

通过execute方法向线程池中提交Runnable任务时,流程如下:
客户端每提交一个任务,线程池就会在核心线程池中创建一个工作线程来执行这个任务,当核心线程中的线程已满时,就会将任务存储到任务队列中,等待核心线程池中的空闲线程来执行,如果任务队列满了线程会在工厂创建临时线程来执行任务,直到当前线程池总线程数量超过最大线程数时就会执行拒绝策略

2.线程池处理Callable任务
public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                5,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

       //public Future<T> submit(Callable<T> task); 执行Callable任务,返回未来任务对象,用来获取线程完毕返回的结果
        Future<Integer> ft1 = pool.submit(new MyCallable(10)); 
        Future<Integer> ft2 = pool.submit(new MyCallable(50)); 
        Future<Integer> ft3 = pool.submit(new MyCallable(100)); 

        System.out.println(ft1.get()); //55
        System.out.println(ft2.get()); //1275
        System.out.println(ft3.get()); //5050
        pool.shutdown();

    }
}

//求1到n的整数和,并返回结果
class MyCallable implements Callable<Integer>{
    //通过带参构造,注入一些数据
    private int n;

    public MyCallable() {

    }

    public MyCallable(int n){
        this.n = n;
    }

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= n ; i++) {
            sum += i;
        }
        return sum;
    }
}

在这里插入图片描述

通过submit方法向线程池中提交Callable任务时,流程如下:
客户端每提交一个任务,线程池就会在核心线程池中创建一个工作线程来执行这个任务,当核心线程中的线程已满时,就会将任务存储到任务队列中,等待核心线程池中的空闲线程来执行,如果任务队列满了线程会在工厂创建临时线程来执行任务,直到当前线程池总线程数量超过最大线程数时就会执行拒绝策略

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值