• 向线程池提交任务


    (1)  ThreadPoolExetor#submit(Callable<T> task)   有返回值

    public class Sumit1 {
    
        public static void main(String[] args)  {
            Callable<String> callable = new Callable<String>() {
                public String call() throws Exception {
                    System.out.println("This is ThreadPoolExetor#submit(Callable<T> task) method.");
                    Thread.sleep(5000);
                    int i = 1/0;
                    return "result";
                }
            };
            
            ExecutorService executor = Executors.newSingleThreadExecutor();
            Future<String> future = executor.submit(callable);
            
            try {
                System.out.println(future.get());//(1)这里会阻塞,一直会等待future返回结果,(2)当用于计算的线程发生异常了,也会抛出异常
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            System.out.println("##########");
    
        }
    }

    (2) ThreadPoolExetor#submit(Runnable task, T result)   有返回值,返回值是通过result间接获取的

    /**
     * 
     *
     * 在给submit传递参数时,第二个参数就是future的返回值,当传递的是"abc"时,从future获取的也是"abc"
     *
     * 假如在runnable的构造函数传递的和submit第二个参数是同一个data
     * ,则在runnable线程中对data的修改后,从future获取结果后也可知道data修改的地方
     */
    public class Submit2 {
    
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            ExecutorService executor = Executors.newSingleThreadExecutor();
            Data data = new Data();
            System.out.println("before:" + data.hashCode() + " ,  name = " + data.getName());
            Future<Data> future = executor.submit(new Task(data), data);
            System.out.println("after:" + future.get().hashCode() + " ,  name = " + future.get().getName()); // 这里会阻塞,
    
        }
    
    }
    
    class Data {
        String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    class Task implements Runnable {
        Data data;
    
        public Task(Data data) {
            this.data = data;
        }
    
        public void run() {
            System.out.println("This is ThreadPoolExetor#submit(Runnable task, T result) method.");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            data.setName("kevin");
        }
    }

     (3)ThreadPoolExetor#submit(Runnable runnable)    没有返回值,获取的nulll

    public class Submit3 {
    
        public static void main(String[] args) {
            
            Runnable runnable = new Runnable() {
                public void run() {
                    int i = 1/0;
                    System.out.println("This is ThreadPoolExetor#submit(Runnable runnable) method.");
                }
            };
    
            ExecutorService executor = Executors.newSingleThreadExecutor();
            Future<?> future = executor.submit(runnable);
            try {
                System.out.println(future.get());
            } catch (InterruptedException | ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("#########");
            
        }
    }

    (4)ThreadPoolExetor#execute(Runnable runnable)  

    /**
     * 
     * 采用execute方法,执行任务的线程抛出异常后,当前线程不能捕获该异常
     *
     */
    public class Submit4 {
    
        public static void main(String[] args) {
            
            Runnable runnable = new Runnable() {
                public void run() {
                    int i = 1/0;
                    
                    System.out.println("This is ThreadPoolExetor#submit(Runnable runnable) method.");
                }
            };
    
            ExecutorService executor = Executors.newSingleThreadExecutor();
            executor.execute(runnable);
            System.out.println("########");
            
        }
    }

    如何选择这几种方式

    当希望返回任务线程的结果时或者想知道任务线程有没有发生异常,选择submit方法

    对任务线程是否发生异常不关心时,选择execute方法

  • 相关阅读:
    贰、js的基础(二)类型转换
    贰、js的基础(一)
    ajax的异步请求小结
    壹、js的概述
    sass的用法小结(四)进阶篇
    sass的用法小结(三)
    sass的用法小结(二)
    sass的用法小结(一)
    H5页面在线制作工具搜集
    H5教程:移动页面性能优化
  • 原文地址:https://www.cnblogs.com/moris5013/p/11350169.html
Copyright © 2020-2023  润新知