在Java中设计并发线程时,使用Runnable接口和Callable接口有什么区别,为什么你会选择其中一个而不是另一个?
当前回答
让我们看看在哪里可以使用Runnable和Callable。
Runnable和Callable都运行在与调用线程不同的线程上。但是Callable可以返回值,而Runnable不能。那么这到底适用于什么呢?
Runnable:如果你有一个火和忘记任务,那么使用Runnable。将代码放在Runnable中,当run()方法被调用时,就可以执行任务了。调用线程实际上并不关心您何时执行任务。
Callable:如果您试图从任务中检索值,则使用Callable。可调用本身是做不到的。你将需要一个Future,你将它包裹在你的Callable中,并获得关于Future的价值。get()。在这里,调用线程将被阻塞,直到Future返回结果,然后等待Callable的call()方法执行。
因此,考虑一个到目标类的接口,其中定义了Runnable和Callable包装方法。调用类会随机调用你的接口方法,不知道哪个是Runnable,哪个是Callable。Runnable方法将异步执行,直到Callable方法被调用。这里调用类的线程将阻塞,因为您正在从目标类中检索值。
注意:在目标类内部,您可以在单个线程执行器上调用Callable和Runnable,使此机制类似于串行调度队列。因此,只要调用者调用你的Runnable包装方法,调用线程就会非常快地执行而不会阻塞。一旦它调用了一个包装在Future方法中的Callable,它将不得不阻塞,直到所有其他排队的项都被执行。只有这样,该方法才会返回值。这是一种同步机制。
其他回答
oracle文档中这些接口的用途:
可运行接口应该由任何类实现,其实例将由线程执行。该类必须定义一个名为run的无参数方法。
可调用:返回结果并可能抛出异常的任务。实现者定义一个不带参数的方法叫做call。 Callable接口与Runnable接口类似,因为两者都是为其实例可能由另一个线程执行的类设计的。然而,Runnable不返回结果,也不能抛出受控异常。
其他的差异:
You can pass Runnable to create a Thread. But you can't create new Thread by passing Callable as parameter. You can pass Callable only to ExecutorService instances. Example: public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } } Use Runnable for fire and forget calls. Use Callable to verify the result. Callable can be passed to invokeAll method unlike Runnable. Methods invokeAny and invokeAll perform the most commonly useful forms of bulk execution, executing a collection of tasks and then waiting for at least one, or all, to complete Trivial difference : method name to be implemented => run() for Runnable and call() for Callable.
可调用的和可运行的都彼此相似,可以在实现线程中使用。在实现Runnable的情况下,你必须实现run()方法,但在可调用的情况下,你必须实现call()方法,这两种方法的工作方式相似,但可调用的call()方法有更大的灵活性。他们之间有一些不同。
Runnable和callable之间的区别如下所示
1) runnable的run()方法返回void,这意味着如果你想让你的线程返回一些你可以进一步使用的东西,那么你没有选择runnable run()方法。有一个解决方案'Callable',如果你想返回任何形式的对象,那么你应该使用Callable而不是Runnable。可调用接口有方法'call()',该方法返回Object。
方法签名- 可运行- >
public void run(){}
可调用的- - - >
public Object call(){}
2)对于Runnable run()方法,如果出现任何检查异常,那么你必须用try catch块处理,但对于Callable call()方法,你可以抛出检查异常,如下所示
public Object call() throws Exception {}
3) Runnable来自遗留的java 1.0版本,而callable来自java 1.5版本的Executer框架。
如果你熟悉Executers,那么你应该使用Callable而不是Runnable。
希望你能理解。
Java功能接口
它是一种与函数式编程相匹配的接口命名约定
//Runnable
interface Runnable {
void run();
}
//Action - throws exception
interface Action {
void run() throws Exception;
}
//Consumer - consumes a value/values, throws exception
//BiConsumer,
interface Consumer1<T> {
void accept(T t) throws Exception;
}
//Callable - return result, throws exception
interface Callable<R> {
R call() throws Exception;
}
//Supplier - returns result, throws exception
interface Supplier<R> {
R get() throws Exception;
}
//Predicate - consumes a value/values, returns true or false, throws exception
interface Predicate1<T> {
boolean test(T t) throws Exception;
}
//Function - consumes a value/values, returns result, throws exception
//BiFunction, Function3...
public interface Function1<T, R> {
R apply(T t) throws Exception;
}
...
//Executor
public interface Executor {
void execute(Runnable command);
}
【快速闭包命名】
除了所有其他答案:
我们不能传递/使用Callable到单独的线程执行,即Callable只能在执行器框架中使用。 但是,Runnable可以传递给一个单独的线程执行(new thread (new CustomRunnable())),也可以在Executor Framework中使用。
Runnable和Callable在应用程序中的区别是什么?是否只与返回参数在Callable中存在差异?
基本上,是的。请看这个问题的答案。Callable的javadoc。
如果Callable能做Runnable能做的所有事情,那么两者都需要什么呢?
因为Runnable接口不能做Callable所做的所有事情!
Runnable has been around since Java 1.0, but Callable was only introduced in Java 1.5 ... to handle use-cases that Runnable does not support. In theory, the Java team could have changed the signature of the Runnable.run() method, but this would have broken binary compatiblity with pre-1.5 code, requiring recoding when migrating old Java code to newer JVMs. That is a BIG NO-NO. Java strives to be backwards compatible ... and that's been one of Java's biggest selling points for business computing.
显然,在某些用例中,任务不需要返回结果或抛出检查过的异常。对于这些用例,使用Runnable比使用Callable<Void>和从call()方法返回一个虚拟(null)值更简洁。
推荐文章
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- 跨线程操作无效:控件“textBox1”从创建它的线程以外的线程访问
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?