ExcectorService中的Submit(Runnable)和Submit(Callable)之间的区别?

时间:2019-02-13 14:31:18

标签: java multithreading executorservice

下面是实现Runnable类的代码

public class ExecutorServiceWithRunnable {

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        Future<emp> submit =null;
        emp emp = new emp();
        for(int i=0;i<5;i++) {
             submit = (Future<emp>) pool.submit(new RunnableClass(emp));
             try {
                    System.out.println(submit.get().getId());
                } catch (InterruptedException | ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }


    }
}
class RunnableClass implements Runnable{

    emp e=null;
    public RunnableClass(emp emp) {
        // TODO Auto-generated constructor stub
        this.e=emp;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName()+"...."+e.getId());
        e.setId(e.getId()+1);

    }

}
class emp{
    int id;
    String status;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

发布权是:pool-1-thread-1 .... 0线程“ main”中的异常 ExecutorServiceWithRunnableCallable.main(ExecutorServiceWithRunnable.java:15)处的java.lang.NullPointerException

下面是Callable的代码

public class ExecutorServiceWithRunnableCallable2 {

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        Future<emp> submit =null;
        emp emp = new emp();
        for(int i=0;i<5;i++) {
             submit = pool.submit(new CallableClass(emp));
             try {
                System.out.println(submit.get().getId());
            } catch (InterruptedException | ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }
}
class CallableClass implements Callable<emp>{
    emp e=null;
    public CallableClass(emp emp) {
        // TODO Auto-generated constructor stub
        this.e=emp;
    }

    @Override
    public emp call() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName()+"...."+e.getId());
        e.setId(e.getId()+1);
        return e;
    }

}

** Got输出为:** pool-1-thread-1 .... 0 1个 池1线程2 .... 1 2 池1线程3 .... 2 3 池1线程4 .... 3 4 池1线程5 .... 4 5

因此请在两种情况下说明我的Submit方法返回的将来对象。

1 个答案:

答案 0 :(得分:4)

Runnable不返回结果。因此,Future::get仍然会阻塞,直到可运行结束,然后才返回null

submit = (Future<emp>) pool.submit(new RunnableClass(emp));

此类广播将产生警告,因为您实际上在这里没有得到Future<emp>。而且因为您忽略了这一点,所以以后您就获得了例外。

就像Javadoc所说的那样,您实际上得到了Future<?>,这意味着(在这种情况下)您不能使用结果值。

submit = pool.submit(new CallableClass(emp));

如果您想要结果,请提交Callable。请注意,不需要进行类强制转换,它可以从Callable的类型推断出Future的通用类型。