通过Callable接口实现多线程

 

通过Callable接口实现多线程

  460人阅读  评论(0)  收藏  举报
  分类:

之前通过继承Thread类实现多线程,通过Runnable接口+静态代理实现多线程

但是有一个不足之处是,重写run方法时没有返回值也不能抛出异常

使用Callable接口就可以解决这个问题


Callable接口和Runnable接口的不同之处:

1.Callable规定的方法是call,而Runnable是run

2.call方法可以抛出异常,但是run方法不行

3.Callable对象执行后可以有返回值,运行Callable任务可以得到一个Future对象,通过Future对象可以了解任务执行情况,可以取消任务的执行,而Runnable不可有返回值




多线程的实现有以下4个步骤:


1.创建一个线程,创建Callable的实现类Race,并且重写call方法

ExecutorService ser=Executors.newFixedThreadPool(线程数目);
Race tortoise = new Race();


2.得到Future对象

Future<Integer> result=ser.submit(tortoise);


3.获取返回值

int num=result.get();


4.停止服务

ser.shutdown();





举一个龟兔赛跑的例子,直接上代码吧

[java]  view plain  copy
 print ?
  1. public class Call {  
  2.       
  3.     public static void main(String[] args) throws InterruptedException, ExecutionException {  
  4.           
  5.         ExecutorService ser=Executors.newFixedThreadPool(2);  
  6.           
  7.         Race tortoise=new Race("tortoise",1000);  
  8.         Race rabbit = new Race("rabbit",500);  
  9.           
  10.           
  11.         //获取值  
  12.         Future<Integer> resultR=ser.submit(rabbit);  
  13.         Future<Integer> resultT=ser.submit(tortoise);  
  14.           
  15.         Thread.sleep(3000);  
  16.         rabbit.setFlag(false);  
  17.         tortoise.setFlag(false);  
  18.           
  19.         int numR=resultR.get();  
  20.         int numT=resultT.get();  
  21.           
  22.         System.out.println("rabbit  ---"+numR);  
  23.         System.out.println("tortoise  ---"+numT);  
  24.           
  25.         //停止服务  
  26.         ser.shutdown();  
  27.     }  
  28. }  
  29.   
  30. class Race implements Callable<Integer>{  
  31.       
  32.     private int step=0;  
  33.     private String name;  
  34.     private long time;  
  35.     private boolean flag=true;  
  36.       
  37.     Race(String name,long time){  
  38.         this.name=name;  
  39.         this.time=time;  
  40.     }  
  41.   
  42.     @Override  
  43.     public Integer call() throws Exception {  
  44.         while(flag){  
  45.             Thread.sleep(time);  
  46.             step++;  
  47.         }  
  48.         return step;  
  49.     }  
  50.       
  51.     public void setFlag(boolean flag){  
  52.         this.flag=flag;  
  53.     }  
  54. }  



1
0
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在中,可以通过实现Callable接口来创建一个带返回值的线程任务。与Runnable接口不同,Callable接口的call()方法可以返回一个结果,而且可以抛出异常。 下面是一个简单的例子: ```java import java.util.concurrent.Callable; public class MyCallable implements Callable<String> { public String call() throws Exception { // 在这里编写你的多线程代码 return "Hello World"; } } ``` 在上面的代码中,我们实现Callable接口,并且重写了call()方法。在这个方法中,我们可以编写我们的多线程代码,并且使用return语句返回一个结果。 然后,我们可以在主线程中使用Callable创建一个Future对象,以便在后面获取线程的结果。下面是一个使用Future的例子: ```java import java.util.concurrent.*; public class Main { public static void main(String[] args) throws Exception { // 创建一个ExecutorService,用于执行Callable任务 ExecutorService executor = Executors.newSingleThreadExecutor(); // 创建一个Callable任务 Callable<String> callable = new MyCallable(); // 提交任务并返回一个Future对象 Future<String> future = executor.submit(callable); // 等待任务执行完成并获取结果 String result = future.get(); // 输出结果 System.out.println(result); // 关闭ExecutorService executor.shutdown(); } } ``` 在上面的代码中,我们首先创建了一个ExecutorService,它用于执行Callable任务。然后,我们创建了一个Callable任务,并将其提交给ExecutorService。这个方法返回一个Future对象,我们可以使用它来等待任务执行完成并获取结果。最后,我们输出结果,并关闭ExecutorService。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值