`
uule
  • 浏览: 6359376 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

Callable接口和Runnable接口

 
阅读更多

 1、

public interface Executor {

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the <tt>Executor</tt> implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution.
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

 

//接口ExecutorService继承自Executor,它的目的是为我们管理Thread对象,从而简化并发编程
public interface ExecutorService extends Executor {

    <T> Future<T> submit(Callable<T> task);
   
    <T> Future<T> submit(Runnable task, T result);

    Future<?> submit(Runnable task);
	
    ...   
}

 

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;
}


public interface Runnable {
   
    public abstract void run();
}

 

public interface Future<V> {
    
    boolean cancel(boolean mayInterruptIfRunning);    

    /**
     * Waits if necessary for the computation to complete, and then
     * retrieves its result.
     *
     * @return the computed result  
     */
    V get() throws InterruptedException, ExecutionException;

   
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}

 

Callable接口和Runnable接口相似,区别就是Callable需要实现call方法,而Runnable需要实现run方法;并且,call方法还可以返回任何对象,无论是什么对象,JVM都会当作Object来处理。但是如果使用了泛型,我们就不用每次都对Object进行转换了。

 

Runnable和Callable都是接口

不同之处:
1.Callable可以返回一个类型V,而Runnable不可以
2.Callable能够抛出checked exception,而Runnable不可以
3.Runnable是自从java1.1就有了,而Callable是1.5之后才加上去的
4.Callable和Runnable都可以应用于executors。而Thread类只支持Runnable.
上面只是简单的不同,其实这两个接口在用起来差别还是很大的。Callable与executors联合在一起,在任务完成时可立刻获得一个更新了的Future。而Runable却要自己处理

 

  Future接口,一般都是取回Callable执行的状态用的。其中的主要方法:

  • cancel,取消Callable的执行,当Callable还没有完成时
  • get,获得Callable的返回值
  • isCanceled,判断是否取消了
  • isDone,判断是否完成

 

用Executor来构建线程池,应该要做的事:

1).调用Executors类中的静态方法newCachedThreadPool(必要时创建新线程,空闲线程会被保留60秒)或newFixedThreadPool(包含固定数量的线程池)等,返回的是一个实现了ExecutorService接口的ThreadPoolExecutor类或者是一个实现了ScheduledExecutorServiece接口的类对象。

2).调用submit提交Runnable或Callable对象。

3).如果想要取消一个任务,或如果提交Callable对象,那就要保存好返回的Future对象。

4).当不再提交任何任务时,调用shutdown方法

 

举2个例子如下:

Java代码   收藏代码
  1. package thread.test04;  
  2. import java.util.concurrent.*;  
  3. public class ThreadTestA {  
  4.     public static void main(String[] args) {  
  5.         ExecutorService e=Executors.newFixedThreadPool(10);  
  6.         e.execute(new MyRunnableA());  
  7.         e.execute(new MyRunnableB());  
  8.        e.shutdown();  
  9.    }  
  10.   
  11. }  
  12.   
  13. class MyRunnableA implements Runnable{  
  14.       
  15.     public void run(){  
  16.         System.out.println("Runnable:run()....");  
  17.         int i=0;  
  18.         while(i<20){  
  19.             i++;  
  20.             for(int j=0;j<1000000;j++);  
  21.             System.out.println("i="+i);  
  22.         }  
  23.     }  
  24. }  
  25.   
  26. class MyRunnableB implements Runnable{  
  27.     public void run(){  
  28.         char c='A'-1;  
  29.         while(c<'Z'){  
  30.             c++;  
  31.             for(int j=0;j<1000000;j++);  
  32.             System.out.println("c="+c);  
  33.         }  
  34.     }  
  35. }  

 

Java代码   收藏代码
  1. package thread.test04;  
  2.   
  3. import java.util.concurrent.Callable;  
  4. import java.util.concurrent.ExecutionException;  
  5. import java.util.concurrent.ExecutorService;  
  6. import java.util.concurrent.Executors;  
  7. import java.util.concurrent.Future;  
  8.   
  9. public class ThreadTestB {  
  10.     public static void main(String[] args) {  
  11.         ExecutorService e=Executors.newFixedThreadPool(10);  
  12.         Future f1=e.submit(new MyCallableA());  
  13.         Future f2=e.submit(new MyCallableA());  
  14.         Future f3=e.submit(new MyCallableA());        
  15.         System.out.println("--Future.get()....");  
  16.         try {  
  17.             System.out.println(f1.get());  
  18.             System.out.println(f2.get());  
  19.             System.out.println(f3.get());            
  20.         } catch (InterruptedException e1) {  
  21.             e1.printStackTrace();  
  22.         } catch (ExecutionException e1) {  
  23.             e1.printStackTrace();  
  24.         }  
  25.           
  26.         e.shutdown();  
  27.           
  28.     }  
  29.   
  30. }  
  31.   
  32. class MyCallableA implements Callable<String>{  
  33.     public String call() throws Exception {  
  34.         System.out.println("开始执行Callable");  
  35.         String[] ss={"zhangsan","lisi"};  
  36.         long[] num=new long[2];  
  37.         for(int i=0;i<1000000;i++){  
  38.             num[(int)(Math.random()*2)]++;  
  39.         }  
  40.           
  41.         if(num[0]>num[1]){  
  42.             return ss[0];  
  43.         }else if(num[0]<num[1]){  
  44.             throw new Exception("弃权!");  
  45.         }else{  
  46.             return ss[1];  
  47.         }  
  48.     }  
  49.       
  50. }  

 来源:http://junlas.iteye.com/blog/846457

 

/**
 * Factory and utility methods for {@link Executor}, {@link
 * ExecutorService}, {@link ScheduledExecutorService}, {@link
 * ThreadFactory}, and {@link Callable} classes defined in this
 * package. This class supports the following kinds of methods:
 *
 * <ul>
 *   <li> Methods that create and return an {@link ExecutorService}
 *        set up with commonly useful configuration settings.
 *   <li> Methods that create and return a {@link ScheduledExecutorService}
 *        set up with commonly useful configuration settings.
 *   <li> Methods that create and return a "wrapped" ExecutorService, that
 *        disables reconfiguration by making implementation-specific methods
 *        inaccessible.
 *   <li> Methods that create and return a {@link ThreadFactory}
 *        that sets newly created threads to a known state.
 *   <li> Methods that create and return a {@link Callable}
 *        out of other closure-like forms, so they can be used
 *        in execution methods requiring <tt>Callable</tt>.
 * </ul>
 *
 * @since 1.5
 * @author Doug Lea
 */
public class Executors {

    /**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * <tt>nThreads</tt> threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if <tt>nThreads &lt;= 0</tt>
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

    /**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * ThreadFactory to create new threads when needed.  At any point,
     * at most <tt>nThreads</tt> threads will be active processing
     * tasks.  If additional tasks are submitted when all threads are
     * active, they will wait in the queue until a thread is
     * available.  If any thread terminates due to a failure during
     * execution prior to shutdown, a new one will take its place if
     * needed to execute subsequent tasks.  The threads in the pool will
     * exist until it is explicitly {@link ExecutorService#shutdown
     * shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @param threadFactory the factory to use when creating new threads
     * @return the newly created thread pool
     * @throws NullPointerException if threadFactory is null
     * @throws IllegalArgumentException if <tt>nThreads &lt;= 0</tt>
     */
    public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);
    }

    /**
     * Creates an Executor that uses a single worker thread operating
     * off an unbounded queue. (Note however that if this single
     * thread terminates due to a failure during execution prior to
     * shutdown, a new one will take its place if needed to execute
     * subsequent tasks.)  Tasks are guaranteed to execute
     * sequentially, and no more than one task will be active at any
     * given time. Unlike the otherwise equivalent
     * <tt>newFixedThreadPool(1)</tt> the returned executor is
     * guaranteed not to be reconfigurable to use additional threads.
     *
     * @return the newly created single-threaded Executor
     */
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

    /**
     * Creates an Executor that uses a single worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed. Unlike the otherwise
     * equivalent <tt>newFixedThreadPool(1, threadFactory)</tt> the
     * returned executor is guaranteed not to be reconfigurable to use
     * additional threads.
     *
     * @param threadFactory the factory to use when creating new
     * threads
     *
     * @return the newly created single-threaded Executor
     * @throws NullPointerException if threadFactory is null
     */
    public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>(),
                                    threadFactory));
    }

    /**
     * Creates a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.  These pools will typically improve the performance
     * of programs that execute many short-lived asynchronous tasks.
     * Calls to <tt>execute</tt> will reuse previously constructed
     * threads if available. If no existing thread is available, a new
     * thread will be created and added to the pool. Threads that have
     * not been used for sixty seconds are terminated and removed from
     * the cache. Thus, a pool that remains idle for long enough will
     * not consume any resources. Note that pools with similar
     * properties but different details (for example, timeout parameters)
     * may be created using {@link ThreadPoolExecutor} constructors.
     *
     * @return the newly created thread pool
     */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

    /**
     * Creates a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available, and uses the provided
     * ThreadFactory to create new threads when needed.
     * @param threadFactory the factory to use when creating new threads
     * @return the newly created thread pool
     * @throws NullPointerException if threadFactory is null
     */
    public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }

    /**
     * Creates a single-threaded executor that can schedule commands
     * to run after a given delay, or to execute periodically.
     * (Note however that if this single
     * thread terminates due to a failure during execution prior to
     * shutdown, a new one will take its place if needed to execute
     * subsequent tasks.)  Tasks are guaranteed to execute
     * sequentially, and no more than one task will be active at any
     * given time. Unlike the otherwise equivalent
     * <tt>newScheduledThreadPool(1)</tt> the returned executor is
     * guaranteed not to be reconfigurable to use additional threads.
     * @return the newly created scheduled executor
     */
    public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }

    /**
     * Creates a single-threaded executor that can schedule commands
     * to run after a given delay, or to execute periodically.  (Note
     * however that if this single thread terminates due to a failure
     * during execution prior to shutdown, a new one will take its
     * place if needed to execute subsequent tasks.)  Tasks are
     * guaranteed to execute sequentially, and no more than one task
     * will be active at any given time. Unlike the otherwise
     * equivalent <tt>newScheduledThreadPool(1, threadFactory)</tt>
     * the returned executor is guaranteed not to be reconfigurable to
     * use additional threads.
     * @param threadFactory the factory to use when creating new
     * threads
     * @return a newly created scheduled executor
     * @throws NullPointerException if threadFactory is null
     */
    public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1, threadFactory));
    }

    /**
     * Creates a thread pool that can schedule commands to run after a
     * given delay, or to execute periodically.
     * @param corePoolSize the number of threads to keep in the pool,
     * even if they are idle.
     * @return a newly created scheduled thread pool
     * @throws IllegalArgumentException if <tt>corePoolSize &lt; 0</tt>
     */
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

    /**
     * Creates a thread pool that can schedule commands to run after a
     * given delay, or to execute periodically.
     * @param corePoolSize the number of threads to keep in the pool,
     * even if they are idle.
     * @param threadFactory the factory to use when the executor
     * creates a new thread.
     * @return a newly created scheduled thread pool
     * @throws IllegalArgumentException if <tt>corePoolSize &lt; 0</tt>
     * @throws NullPointerException if threadFactory is null
     */
    public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }
    ........

    /** Cannot instantiate. */
    private Executors() {}
}

 

分享到:
评论

相关推荐

    使用Runnable模拟Callable接口实现线程有返回值

    大家都知道Runnable和Callable接口都可以作为其他线程执行的任务,但是Runnable接口的run方法没有返回值,而Callable接口的call方法有返回值,那么Callable接口是如何做到的呢?在此我给出一个Demo,看看通过...

    Callable接口源码阅读1

    Callable接口在Java中扮演着重要的角色,特别是在多线程编程中。...通过lambda表达式的使用,实现Callable接口的任务变得更加简洁和直观。在实际开发中,合理利用Callable接口可以提高代码的可读性和效率。

    详解Java Callable接口实现多线程的方式

    Callable接口和Runnable接口都是用于多线程的接口,但是它们有所不同。Runnable接口没有返回值,而Callable接口可以返回执行结果。Runnable接口的run()方法返回void类型,而Callable接口的call()方法可以返回任何...

    线程池的实现以及底层原理.docx

    Callable接口和Runnable接口都是Java中的线程接口,但是它们有所不同: 1. Callable接口:Callable接口可以返回执行结果,可以抛出检查异常,适用于需要返回结果或抛出异常的场景。 2. Runnable接口:Runnable接口...

    MapReduce,泛型,匿名内部类,Runnable和Callable

    在Java编程中,泛型是一种强大的特性,允许我们在类、接口和方法中使用类型参数。这提供了编译时的类型安全,减少了类型转换的错误,并提高了代码的可读性和重用性。例如,在集合框架中,通过泛型我们可以确保集合只...

    Java使用Callable和Future创建线程操作示例

    首先,Java 5开始,Java提供了Callable接口,该接口是Runnable接口的增强版,Callable接口提供了一个call()方法,可以看作是线程的执行体,但call()方法比run()方法更强大。call()方法可以有返回值。call()方法可以...

    【Java】Callable创建线程用到的适配器模式(csdn)————程序.pdf

    首先,Callable接口与Runnable接口类似,都是用于创建新线程的接口。但是,Runnable接口的run()方法无返回值,而Callable的call()方法可以返回一个结果,并且允许在计算过程中抛出异常。在上述示例中,`MyThread`类...

    Android(Java)之多线程结果返回——Future 、FutureTask、Callable、Runnable

    `Future`、`FutureTask`、`Callable`和`Runnable`是Java并发编程中的核心接口和类,它们在Android开发中同样有着广泛的应用。下面将详细介绍这些概念以及它们如何协同工作。 1. `Runnable`: 这是Java中最基础的多...

    Java中的Callable接口最全讲义

    Callable接口的基本用法 2.1 创建Callable任务 2.2 提交Callable任务 2.3 获取任务执行结果 Future接口的使用 3.1 获取任务执行状态 3.2 取消任务的执行 Callable与Runnable的对比 Callable的异常处理 Callable的...

    Java Callable接口实现细节详解

    通过使用 Callable 接口和 FutureTask 类,可以实现复杂的异步任务,并且可以获取任务的执行结果。 知识点 * Callable 接口的定义和实现 * FutureTask 类的使用 *异步任务的执行和结果获取 *线程的缓存和阻塞问题 ...

    Java多线程之Callable接口的实现

    Callable接口与Runnable接口的区别在于Callable接口可以返回结果并抛出异常,而Runnable接口不能返回结果也不能抛出异常。 在实现Callable接口时,我们需要重写call()方法,该方法定义了要执行的任务。例如,我们...

    Java中的Runnable,Callable,Future,FutureTask的比较

    2. **Callable**: 相比Runnable,Callable接口提供了更强大的功能,它包含一个带返回值的`call()`方法。这使得Callable任务能够返回计算结果或抛出异常。例如,如果一个任务需要计算一个复杂的数学问题,Callable...

    Java 线程对比(Thread,Runnable,Callable)实例详解

    在Java中,有三种常见的创建线程的方式:继承Thread类、实现Runnable接口以及使用Callable接口。下面将对这三种方式的使用、优缺点进行详细对比。 1. 继承Thread类 这种方式是最直接的创建线程的方法,通过创建...

    Java多线程Callable接口实现代码示例

    从 Callable 的定义可以看出,Callable 接口类似于 Runnable 接口,两者都是为那些其实例可能被另一个线程执行的类设计的。方法可以有返回值,并且可以抛出异常。但是,Runnable 不可以返回结果。 Callable 需要...

    Java多线程Callable接口

    Runnable是执行工作的独立任务,但是它不返回任何值,如果你希望任务在完成时能够返回一个值,那么可以实现Callable接口而不是Runnable接口。在Java SE5中引入的Callable是一种具有类型参数的泛型,它的类型参数表示...

    Java多线程Callable和Future接口区别

    这是Callable和Runnable接口最大的区别。 Future接口是Java多线程编程中用于描述异步计算的结果的接口。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。通过Future对象可了解任务执行情况,...

    Java多线程实现Callable接口

    在Java中,实现多线程有两种主要方式:继承Thread类和实现Runnable接口。此外,Java 5引入了Callable接口,它提供了一种更灵活的方式来创建线程,特别是当需要从线程中返回结果时。 Callable接口类似于Runnable,但...

    JAVA中Callable的使用

    与Runnable接口相比,Callable接口提供了更强大的功能,因为Runnable只能定义无返回值的任务,而Callable可以定义有返回值的任务。这篇教程将深入探讨如何在Java中使用Callable。 Callable接口位于`java.util....

    创建线程的三种方式(Thread、Runnable、Callable).docx

    在Java编程中,创建线程主要有三种方式:继承Thread类、实现Runnable接口以及实现Callable接口。下面我们将逐一探讨这些方式的细节。 **方式一:继承Thread类实现多线程** 这种方式是最直观的创建线程的方法。首先...

Global site tag (gtag.js) - Google Analytics