6.3 线程池的execute()和submit()的区别和使用示例

execute():提交不需要返回值的任务,无法判断任务是否被执行成功了。

submit():提交需要放回值的任务;线程会返回Future对象,通过Future的isDone()方法可以判断任务是否执行成功,并且可以通过Future.get()获取返回的值,get方法会阻塞,直到线程的完成

get(long timeout, TimeUnit unit)会在等待一段时间后返回,这段时间内任务可能没有执行完成。

以下是execute()和submit():使用的简单demo

{
    static class Customer extends Thread {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "开始执行");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static class Home implements Callable {
        int start;
        int end;

        public Home(int start, int end) {
            this.start = start;
            this.end = end;
        }

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

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10,
                20,
                20,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(10), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                //1、设置线程的名字
                Thread thread = new Thread(r);
                //thread.setName("通过自定义线程工厂谁知线程名字");
                //2、设置成守护线程
                //thread.setDaemon(true);
                return thread;
            }
        },
                //四种阻塞队列满之后的处理方式
//                new ThreadPoolExecutor.CallerRunsPolicy()); 使用调用线程处理,比如是main调用的线程池,用main线程执行
//                new ThreadPoolExecutor.DiscardOldestPolicy());把阻塞队列首元素丢掉,执行当前线程
//                new ThreadPoolExecutor.DiscardPolicy());直接丢掉当前线程
                new AbortPolicy());//跑出异常

        executor.execute(new Customer());
        //计算1加到30
        Future submit1 = executor.submit(new Home(1,10));
        //任务是否执行完成false
        System.out.println(submit1.isDone());
        Thread.sleep(100);
        //任务是否执行完成true
        System.out.println(submit1.isDone());
        Future submit2 = executor.submit(new Home(11,20));
        Future submit3 = executor.submit(new Home(21,30));
        ((Integer)submit1.get()).intValue();
        System.out.println((((Integer)submit1.get()).intValue()+((Integer)submit2.get()).intValue()+((Integer)submit3.get()).intValue()));

    }
}

结果

Thread-1开始执行
false
true
465

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章