【线程池处理任务】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() + "执行了");
    }
}

【线程池处理任务】Runnable任务和Callable任务_第1张图片

通过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 submit(Callable 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;
    }
}

【线程池处理任务】Runnable任务和Callable任务_第2张图片

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

你可能感兴趣的:(java,开发语言)