码迷,mamicode.com
首页 > 其他好文 > 详细

callable和runnable的区别

时间:2020-06-01 01:04:45      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:art   run   ref   detail   net   logs   https   ble   抛出异常   

相同点:

两者都是接口;(废话)
两者都可用来编写多线程程序;
两者都需要调用Thread.start()启动线程;

不同点:

两者最大的不同点是:实现Callable接口的任务线程能返回执行结果;而实现Runnable接口的任务线程不能返回结果;
Callable接口的call()方法允许抛出异常;而Runnable接口的run()方法的异常只能在内部消化,不能继续上抛;

注意点:

Callable接口支持返回执行结果,此时需要调用FutureTask.get()方法实现,此方法会阻塞主线程直到获取‘将来’结果;当不调用此方法时,主线程不会阻塞!

参考:
https://www.cnblogs.com/frinder6/p/5507082.html
https://blog.csdn.net/MrHamster/article/details/89891145

callable和runnable的区别

标签:art   run   ref   detail   net   logs   https   ble   抛出异常   

原文地址:https://www.cnblogs.com/treasury/p/13022489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!