Callable与Future的作用:程序启动一个线程,线程运行完以后有一个返回结果,它可以获得线程运行完之后的返回结果,通过代码看看效果。

  • 1-1 一个Callable与Future的例子
    1. import java.util.*; 
    2. import java.util.concurrent.*; 
    3.  
    4. public class CallableAndFuture { 
    5.     public static void main(String[] args) { 
    6.         ExecutorService threadPool= Executors.newSingleThreadExecutor();//单线程 
    7.         Future<String> future=//submit提交返回的结果,类型是Callable返回的类型 
    8.         threadPool.submit(//提交一个任务 
    9.                 new Callable<String>() { 
    10.                     public String call() throws Exception { 
    11.                         Thread.sleep(2000); 
    12.                         return "hello";//返回的类型是String 
    13.                     }; 
    14.                 } 
    15.         ); 
    16.         System.out.println("等待结果"); 
    17.         try { 
    18.             System.out.println("拿到结果"+future.get());//future.get()获取返回的结果 
    19.           //回来看一下看是否有结果,没有的话抛出异常(线程去干别的事了,而不用一直等待结果) 
    20.           //System.out.println("拿到结果"+future.get(1,TimeUnit.SECONDS)); 
    21.         } catch (InterruptedException e) { 
    22.             e.printStackTrace(); 
    23.         } catch (ExecutionException e) { 
    24.             e.printStackTrace(); 
    25.         } 
    26.          
    27. /*      CompletionService<V>用于提交一组Callable任务,其take方法返回已完成的一个 
    28.         Callable任务对应的Future对象,好比我同是种了几块地麦子,然后就等待收割,收割 
    29.         时,则是那块先成熟了,先去收割那块麦子,而不是哪一块先种,先去收个哪一块。 
    30. */ 
    31.         ExecutorService threadPool2= Executors.newFixedThreadPool(10);//newFixedThreadPool(10)固定大小的线程池,10个 
    32.         //CompletionService<V>是一个接口,应该New它的子类 
    33.         //将线程池传递进去执行它的任务 
    34.         CompletionService<Integer> completionService=new ExecutorCompletionService<Integer>(threadPool2); 
    35.         for (int i = 1; i <= 10; i++) {//提交10任务 
    36.             final int seq=i;//任务的序号 
    37.             completionService.submit(new Callable<Integer>() {//submit()提交任务 
    38.                 @Override 
    39.                 public Integer call() throws Exception { 
    40.                     Thread.sleep(new Random().nextInt(5000));//每隔任务不超过5秒 
    41.                     return seq;//返回任务的序号 
    42.                 } 
    43.             }); 
    44.         } 
    45.          
    46.         //等待收获completionService返回的结果 
    47.         for (int i = 0; i < 10; i++) {//10个任务,需要拿10遍 
    48.             try { 
    49.                 System.out.println( 
    50.                 completionService.take().get());//打印返回的结果 
    51.             } catch (InterruptedException e) { 
    52.                 e.printStackTrace(); 
    53.             } catch (ExecutionException e) { 
    54.                 e.printStackTrace(); 
    55.             } 
    56.         } 
    57.     } 

程序运行的结果:

Java5并发库之Callable与Future的应用_Callable

从以上运行结果可以看出,返回了提交的10的任务的编号。上述代码的整体执行思路:产生线程————向线程提交任务————任务运行完之后,返回任务的结果————获取任务的结果。