Java Callable的用法

来源:互联网 发布:淘宝正品代购店 编辑:程序博客网 时间:2024/06/10 09:23

Callableb必须使用ExecutorService的submit()方法来进行调用,调用后返回Future对象。Future对象调用get方法是线程阻塞的,所以我们可以用isDone()方法来查询Future是否已经完成。

class MyCallable implements Callable<Integer>{        MyCallable(){        }        @Override        public Integer call() throws Exception {            return 66;        }    }
ExecutorService executorService= Executors.newCachedThreadPool();Future<Integer> future = executorService.submit(new MyCallable());try {       Log.e("QRephen", "value = "+ future.get());    } catch (InterruptedException e) {       e.printStackTrace();    } catch (ExecutionException e) {       e.printStackTrace();    }



原创粉丝点击