【说站】Java runnable和callable的异同
2024-12-14
36
Java runnable和callable的异同
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 <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }
@FunctionalInterface public interface Callable<V> { /** * 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; }
以上就是Java runnable和callable的异同,希望对大家有所帮助。更多Java学习指路:Java基础
更新于:1个月前赞一波!3
相关文章
- 【说站】java类加载器的分类
- 【说站】java类的两种引用方法
- 【说站】java转义字符
- 【说站】java基本数据类型
- 【说站】java动态和静态语言的比较
- 【说站】java中Class类的概念介绍
- 【说站】java多态的向上转型是什么
- 【说站】java向下转型是什么意思
- 【说站】java重写发生的条件
- 【说站】java动态绑定怎么用
- 【说站】java多态的好处
- 【说站】java对象池的使用步骤
- 【说站】java向上转型发生的时机
- 【说站】java中变量的使用注意
- 【说站】java类加载的过程
- 【说站】java反射如何调用指定的属性
- 【说站】java RMI的工作过程
- 【说站】java反射机制提供哪些功能
- 【说站】java class实例代表哪些结构
- 【说站】java获取class实例的4种方式
文章评论
评论问答