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; }
作者最新文章
coreldraw快捷键怎么设置在哪里
2026-11-02 21:39
AO3最新可用链接及中文入口汇总
2026-10-29 13:32
coreldraw怎么插入图片 图片导入步骤
2026-10-27 15:37
coreldraw快捷键冲突 怎么改 自定义按键解决
2026-10-24 08:50
AO3中文界面设置指南与核心功能使用教程
2026-10-14 23:28
下一篇:
Java虚拟机的知识点有哪些
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































