您好,欢迎访问一九零五行业门户网

Java中Runnable和Callable的比较

1、相同点
两者都是接口
两者都需要调用thread.start启动线程
2、不同点
callable的核心是call()方法,允许返回值,runnable的核心是run()方法,没有返回值
call()方法可以抛出异常,但是run()方法不行
callable和runnable都可以应用于executors,thread类只支持runnable
3、实例
runnable和callable的接口定义
@functionalinterfacepublic interface runnable {    /**     * when an object implementing interface <code>runnable</code> is used     * to create a thread, starting the thread causes the object's     * <code>run</code> method to be called in that separately executing     * thread.     * <p>     * the general contract of the method <code>run</code> is that it may     * take any action whatsoever.     *     * @see     java.lang.thread#run()     */    public abstract void run();}
@functionalinterfacepublic interface callable<v> {    /**     * computes a result, or throws an exception if unable to do so.     *     * @return computed result     * @throws exception if unable to compute a result     */    v call() throws exception;}
以上就是java中runnable和callable的比较的详细内容。
其它类似信息

推荐信息