码迷,mamicode.com
首页 > 编程语言 > 详细

java.util.concurrent.Callable

时间:2015-08-26 15:59:40      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * A task that returns a result and may throw an exception.
 * Implementors define a single method with no arguments called
 * {@code call}.
 *
 * <p>The {@code Callable} interface is similar to {@link
 * java.lang.Runnable}, in that both are designed for classes whose
 * instances are potentially executed by another thread.  A
 * {@code Runnable}, however, does not return a result and cannot
 * throw a checked exception.
 *
 * <p>The {@link Executors} class contains utility methods to
 * convert from other common forms to {@code Callable} classes.
 *
 * @see Executor
 * @since 1.5
 * @author Doug Lea
 * @param <V> the result type of method {@code call}
 */
@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;

}

Callable 和 Runnable 的使用方法大同小异, 区别在于: 
1.Callable 使用 call() 方法, Runnable 使用 run() 方法 
2.call() 可以返回值, 而 run()方法不能返回。 
3.call() 可以抛出受检查的异常,比如ClassNotFoundException, 而run()不能抛出受检查的异常。 


java.util.concurrent.Callable

标签:

原文地址:http://my.oschina.net/Rayn/blog/497285

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!