
1、相同点
两者都是接口
两者都需要调用Thread.start启动线程
2、不同点
callable的核心是call()方法,允许返回值,runnable的核心是run()方法,没有返回值
call()方法可以抛出异常,但是run()方法不行
callable和runnable都可以应用于executors,thread类只支持runnable
3、实例
Runnable和Callable的接口定义
@FunctionalInterface
publicinterfaceRunnable{
/**
*Whenanobjectimplementinginterface<code>Runnable</code>isused
*tocreateathread,startingthethreadcausestheobject's
*<code>run</code>methodtobecalledinthatseparatelyexecuting
*thread.
*<p>
*Thegeneralcontractofthemethod<code>run</code>isthatitmay
*takeanyactionwhatsoever.
*
*@seejava.lang.Thread#run()
*/
publicabstractvoidrun();
}
@FunctionalInterface
publicinterfaceCallable<V>{
/**
*Computesaresult,orthrowsanexceptionifunabletodoso.
*
*@returncomputedresult
*@throwsExceptionifunabletocomputearesult
*/
Vcall()throwsException;
}原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容