Java中runnable和callable的异同点是什么
作者:归人云淡风轻
时间:2023-04-29
浏览:3
1、相同点两者都是接口两者都需要调用Thread.start启动线程2、不同点callable的核心是call()方法,允许返回值,runnable的核心是run()方法,没有返回值call()方法可以抛出异常,但是run()方法不行callable和runnable都可以应用于executors,thread类只支持runnable3、实例Runnable和Callable的接口定义@FunctionalInterfacepublicinterfaceRunnable{/***Whenanobjecti
本文目录
1、相同点
两者都是接口
两者都需要调用Thread.start启动线程
2、不同点
callable的核心是call()方法,允许返回值,runnable的核心是run()方法,没有返回值
call()方法可以抛出异常,但是run()方法不行
callable和runnable都可以应用于executors,thread类只支持runnable
3、实例
Runnable和Callable的接口定义
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface Runnable is used
* to create a thread, starting the thread causes the object's
* run method to be called in that separately executing
* thread.
*
* The general contract of the method run is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
@FunctionalInterface public interface Callable{ /** * 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; }
作者最新文章
漫蛙manwa防丢网站2026最新地址
2026-11-22 16:55
AO3官网入口 官方登录页面安全直达
2026-11-21 14:10
12306退票后手续费计算异常怎么处理
2026-11-17 11:22
Yandex外贸搜索工具 俄罗斯客户开发官网入口
2026-11-14 11:26
bilibili视频封面怎么替换已发布的
2026-11-12 11:52
下一篇:
Java虚拟机的知识点有哪些
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































