java学习

java学习

 

java实现有返回值的线程

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallAbleTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService service =  Executors.newFixedThreadPool(2);
MyCallAbled m1= new  MyCallAbled("aa");
MyCallAbled m2= new  MyCallAbled("bb");
Future future1 = service.submit(m1);
Future future2 =service.submit(m2);
System.out.println(future1.get().toString());
System.out.println(future2.get().toString());
service.shutdown();
}
static class MyCallAbled implements Callable{
private String name;
public MyCallAbled(String name){
this.name=name;
}
public MyCallAbled(){
}
@Override
public Object call() throws Exception {
return name;
}
}
}

posted on 2017-08-17 17:26 杨军威 阅读(185) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜