Java中runnable和callable的异同点是什么
作者:归人云淡风轻
时间:2023-04-29
浏览:2
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; }
作者最新文章
解决win10运行3DMAX自动重启的方法
2026-10-13 17:08
哔哩哔哩投稿封面上传失败怎么解决
2026-10-13 15:55
漫蛙漫画官网免费漫画入口
2026-10-13 13:19
漫蛙漫画app免费下载安装最新版
2026-10-13 13:18
漫蛙漫画官网登录入口
2026-10-13 13:18
下一篇:
Java虚拟机的知识点有哪些
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多









































