zoukankan      html  css  js  c++  java
  • 线程池的应用及Callable接口的使用

    Java代码 复制代码 收藏代码
    1. public interface Executor {   
    2.   
    3.     /**  
    4.      * Executes the given command at some time in the future.  The command  
    5.      * may execute in a new thread, in a pooled thread, or in the calling  
    6.      * thread, at the discretion of the <tt>Executor</tt> implementation.  
    7.      *  
    8.      * @param command the runnable task  
    9.      * @throws RejectedExecutionException if this task cannot be  
    10.      * accepted for execution.  
    11.      * @throws NullPointerException if command is null  
    12.      */  
    13.     void execute(Runnable command);   
    14. }  
    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);
    }
    Java代码 复制代码 收藏代码
    1. //接口ExecutorService继承自Executor,它的目的是为我们管理Thread对象,从而简化并发编程   
    2. public interface ExecutorService extends Executor {   
    3.   
    4.     <T> Future<T> submit(Callable<T> task);   
    5.       
    6.     <T> Future<T> submit(Runnable task, T result);   
    7.   
    8.     Future<?> submit(Runnable task);   
    9.        
    10.     ...      
    11. }  
    //接口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);
    	
        ...   
    }
    Java代码 复制代码 收藏代码
    1. public interface Callable<V> {   
    2.     /**  
    3.      * Computes a result, or throws an exception if unable to do so.  
    4.      *  
    5.      * @return computed result  
    6.      * @throws Exception if unable to compute a result  
    7.      */  
    8.     V call() throws Exception;   
    9. }   
    10.   
    11.   
    12. public interface Runnable {   
    13.       
    14.     public abstract void run();   
    15. }  
    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();
    }
    Java代码 复制代码 收藏代码
    1. public interface Future<V> {   
    2.        
    3.     boolean cancel(boolean mayInterruptIfRunning);       
    4.   
    5.     /**  
    6.      * Waits if necessary for the computation to complete, and then  
    7.      * retrieves its result.  
    8.      *  
    9.      * @return the computed result    
    10.      */  
    11.     V get() throws InterruptedException, ExecutionException;   
    12.   
    13.       
    14.     V get(long timeout, TimeUnit unit)   
    15.         throws InterruptedException, ExecutionException, TimeoutException;   
    16. }  
    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

    Java代码 复制代码 收藏代码
    1. /**  
    2.  * Factory and utility methods for {@link Executor}, {@link  
    3.  * ExecutorService}, {@link ScheduledExecutorService}, {@link  
    4.  * ThreadFactory}, and {@link Callable} classes defined in this  
    5.  * package. This class supports the following kinds of methods:  
    6.  *  
    7.  * <ul>  
    8.  *   <li> Methods that create and return an {@link ExecutorService}  
    9.  *        set up with commonly useful configuration settings.  
    10.  *   <li> Methods that create and return a {@link ScheduledExecutorService}  
    11.  *        set up with commonly useful configuration settings.  
    12.  *   <li> Methods that create and return a "wrapped" ExecutorService, that  
    13.  *        disables reconfiguration by making implementation-specific methods  
    14.  *        inaccessible.  
    15.  *   <li> Methods that create and return a {@link ThreadFactory}  
    16.  *        that sets newly created threads to a known state.  
    17.  *   <li> Methods that create and return a {@link Callable}  
    18.  *        out of other closure-like forms, so they can be used  
    19.  *        in execution methods requiring <tt>Callable</tt>.  
    20.  * </ul>  
    21.  *  
    22.  * @since 1.5  
    23.  * @author Doug Lea  
    24.  */  
    25. public class Executors {   
    26.   
    27.     /**  
    28.      * Creates a thread pool that reuses a fixed number of threads  
    29.      * operating off a shared unbounded queue.  At any point, at most  
    30.      * <tt>nThreads</tt> threads will be active processing tasks.  
    31.      * If additional tasks are submitted when all threads are active,  
    32.      * they will wait in the queue until a thread is available.  
    33.      * If any thread terminates due to a failure during execution  
    34.      * prior to shutdown, a new one will take its place if needed to  
    35.      * execute subsequent tasks.  The threads in the pool will exist  
    36.      * until it is explicitly {@link ExecutorService#shutdown shutdown}.  
    37.      *  
    38.      * @param nThreads the number of threads in the pool  
    39.      * @return the newly created thread pool  
    40.      * @throws IllegalArgumentException if <tt>nThreads &lt;= 0</tt>  
    41.      */  
    42.     public static ExecutorService newFixedThreadPool(int nThreads) {   
    43.         return new ThreadPoolExecutor(nThreads, nThreads,   
    44.                                       0L, TimeUnit.MILLISECONDS,   
    45.                                       new LinkedBlockingQueue<Runnable>());   
    46.     }   
    47.   
    48.     /**  
    49.      * Creates a thread pool that reuses a fixed number of threads  
    50.      * operating off a shared unbounded queue, using the provided  
    51.      * ThreadFactory to create new threads when needed.  At any point,  
    52.      * at most <tt>nThreads</tt> threads will be active processing  
    53.      * tasks.  If additional tasks are submitted when all threads are  
    54.      * active, they will wait in the queue until a thread is  
    55.      * available.  If any thread terminates due to a failure during  
    56.      * execution prior to shutdown, a new one will take its place if  
    57.      * needed to execute subsequent tasks.  The threads in the pool will  
    58.      * exist until it is explicitly {@link ExecutorService#shutdown  
    59.      * shutdown}.  
    60.      *  
    61.      * @param nThreads the number of threads in the pool  
    62.      * @param threadFactory the factory to use when creating new threads  
    63.      * @return the newly created thread pool  
    64.      * @throws NullPointerException if threadFactory is null  
    65.      * @throws IllegalArgumentException if <tt>nThreads &lt;= 0</tt>  
    66.      */  
    67.     public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {   
    68.         return new ThreadPoolExecutor(nThreads, nThreads,   
    69.                                       0L, TimeUnit.MILLISECONDS,   
    70.                                       new LinkedBlockingQueue<Runnable>(),   
    71.                                       threadFactory);   
    72.     }   
    73.   
    74.     /**  
    75.      * Creates an Executor that uses a single worker thread operating  
    76.      * off an unbounded queue. (Note however that if this single  
    77.      * thread terminates due to a failure during execution prior to  
    78.      * shutdown, a new one will take its place if needed to execute  
    79.      * subsequent tasks.)  Tasks are guaranteed to execute  
    80.      * sequentially, and no more than one task will be active at any  
    81.      * given time. Unlike the otherwise equivalent  
    82.      * <tt>newFixedThreadPool(1)</tt> the returned executor is  
    83.      * guaranteed not to be reconfigurable to use additional threads.  
    84.      *  
    85.      * @return the newly created single-threaded Executor  
    86.      */  
    87.     public static ExecutorService newSingleThreadExecutor() {   
    88.         return new FinalizableDelegatedExecutorService   
    89.             (new ThreadPoolExecutor(1, 1,   
    90.                                     0L, TimeUnit.MILLISECONDS,   
    91.                                     new LinkedBlockingQueue<Runnable>()));   
    92.     }   
    93.   
    94.     /**  
    95.      * Creates an Executor that uses a single worker thread operating  
    96.      * off an unbounded queue, and uses the provided ThreadFactory to  
    97.      * create a new thread when needed. Unlike the otherwise  
    98.      * equivalent <tt>newFixedThreadPool(1, threadFactory)</tt> the  
    99.      * returned executor is guaranteed not to be reconfigurable to use  
    100.      * additional threads.  
    101.      *  
    102.      * @param threadFactory the factory to use when creating new  
    103.      * threads  
    104.      *  
    105.      * @return the newly created single-threaded Executor  
    106.      * @throws NullPointerException if threadFactory is null  
    107.      */  
    108.     public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {   
    109.         return new FinalizableDelegatedExecutorService   
    110.             (new ThreadPoolExecutor(1, 1,   
    111.                                     0L, TimeUnit.MILLISECONDS,   
    112.                                     new LinkedBlockingQueue<Runnable>(),   
    113.                                     threadFactory));   
    114.     }   
    115.   
    116.     /**  
    117.      * Creates a thread pool that creates new threads as needed, but  
    118.      * will reuse previously constructed threads when they are  
    119.      * available.  These pools will typically improve the performance  
    120.      * of programs that execute many short-lived asynchronous tasks.  
    121.      * Calls to <tt>execute</tt> will reuse previously constructed  
    122.      * threads if available. If no existing thread is available, a new  
    123.      * thread will be created and added to the pool. Threads that have  
    124.      * not been used for sixty seconds are terminated and removed from  
    125.      * the cache. Thus, a pool that remains idle for long enough will  
    126.      * not consume any resources. Note that pools with similar  
    127.      * properties but different details (for example, timeout parameters)  
    128.      * may be created using {@link ThreadPoolExecutor} constructors.  
    129.      *  
    130.      * @return the newly created thread pool  
    131.      */  
    132.     public static ExecutorService newCachedThreadPool() {   
    133.         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,   
    134.                                       60L, TimeUnit.SECONDS,   
    135.                                       new SynchronousQueue<Runnable>());   
    136.     }   
    137.   
    138.     /**  
    139.      * Creates a thread pool that creates new threads as needed, but  
    140.      * will reuse previously constructed threads when they are  
    141.      * available, and uses the provided  
    142.      * ThreadFactory to create new threads when needed.  
    143.      * @param threadFactory the factory to use when creating new threads  
    144.      * @return the newly created thread pool  
    145.      * @throws NullPointerException if threadFactory is null  
    146.      */  
    147.     public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {   
    148.         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,   
    149.                                       60L, TimeUnit.SECONDS,   
    150.                                       new SynchronousQueue<Runnable>(),   
    151.                                       threadFactory);   
    152.     }   
    153.   
    154.     /**  
    155.      * Creates a single-threaded executor that can schedule commands  
    156.      * to run after a given delay, or to execute periodically.  
    157.      * (Note however that if this single  
    158.      * thread terminates due to a failure during execution prior to  
    159.      * shutdown, a new one will take its place if needed to execute  
    160.      * subsequent tasks.)  Tasks are guaranteed to execute  
    161.      * sequentially, and no more than one task will be active at any  
    162.      * given time. Unlike the otherwise equivalent  
    163.      * <tt>newScheduledThreadPool(1)</tt> the returned executor is  
    164.      * guaranteed not to be reconfigurable to use additional threads.  
    165.      * @return the newly created scheduled executor  
    166.      */  
    167.     public static ScheduledExecutorService newSingleThreadScheduledExecutor() {   
    168.         return new DelegatedScheduledExecutorService   
    169.             (new ScheduledThreadPoolExecutor(1));   
    170.     }   
    171.   
    172.     /**  
    173.      * Creates a single-threaded executor that can schedule commands  
    174.      * to run after a given delay, or to execute periodically.  (Note  
    175.      * however that if this single thread terminates due to a failure  
    176.      * during execution prior to shutdown, a new one will take its  
    177.      * place if needed to execute subsequent tasks.)  Tasks are  
    178.      * guaranteed to execute sequentially, and no more than one task  
    179.      * will be active at any given time. Unlike the otherwise  
    180.      * equivalent <tt>newScheduledThreadPool(1, threadFactory)</tt>  
    181.      * the returned executor is guaranteed not to be reconfigurable to  
    182.      * use additional threads.  
    183.      * @param threadFactory the factory to use when creating new  
    184.      * threads  
    185.      * @return a newly created scheduled executor  
    186.      * @throws NullPointerException if threadFactory is null  
    187.      */  
    188.     public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {   
    189.         return new DelegatedScheduledExecutorService   
    190.             (new ScheduledThreadPoolExecutor(1, threadFactory));   
    191.     }   
    192.   
    193.     /**  
    194.      * Creates a thread pool that can schedule commands to run after a  
    195.      * given delay, or to execute periodically.  
    196.      * @param corePoolSize the number of threads to keep in the pool,  
    197.      * even if they are idle.  
    198.      * @return a newly created scheduled thread pool  
    199.      * @throws IllegalArgumentException if <tt>corePoolSize &lt; 0</tt>  
    200.      */  
    201.     public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {   
    202.         return new ScheduledThreadPoolExecutor(corePoolSize);   
    203.     }   
    204.   
    205.     /**  
    206.      * Creates a thread pool that can schedule commands to run after a  
    207.      * given delay, or to execute periodically.  
    208.      * @param corePoolSize the number of threads to keep in the pool,  
    209.      * even if they are idle.  
    210.      * @param threadFactory the factory to use when the executor  
    211.      * creates a new thread.  
    212.      * @return a newly created scheduled thread pool  
    213.      * @throws IllegalArgumentException if <tt>corePoolSize &lt; 0</tt>  
    214.      * @throws NullPointerException if threadFactory is null  
    215.      */  
    216.     public static ScheduledExecutorService newScheduledThreadPool(   
    217.             int corePoolSize, ThreadFactory threadFactory) {   
    218.         return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);   
    219.     }   
    220.     ........   
    221.   
    222.     /** Cannot instantiate. */  
    223.     private Executors() {}   
    224. }  
  • 相关阅读:
    js 控制页面跳转的5种方法
    js外部文件问题
    JS设置cookie,删除cookie
    JS中的输出
    转:这些年我们爱犯的弱智错误(菜鸟必看)
    JS 代码调试经验总结(菜鸟必读)
    js中err
    不理解,如果有高手看到了,请帮忙解答,谢谢啦~
    window对象和全局变量,局部变量的疑问
    看script中函数的各种不同形式的结果
  • 原文地址:https://www.cnblogs.com/jianwei-dai/p/5741427.html
Copyright © 2011-2022 走看看