Runnable与Callable

来源:互联网 发布:php 内容过多翻页 编辑:程序博客网 时间:2024/06/02 22:49

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

0 0
原创粉丝点击