/**
* Created with IntelliJ IDEA.
* User: afengzi
* Date: 14-10-9
* Time: 下午5:06
* 实现有返回值的Callable接口
*/
public class Acallable{
public static void main(String args[]){
ExecutorService service = Executors.newCachedThreadPool() ;
List<Future<String>> futures = new ArrayList<Future<String>>() ;
for (int i = 0 ; i< 5 ; i++){
Future<String> future = service.submit(new $Call(100)) ;
futures.add(future) ;
}
service.shutdown();
System.out.println("main -- "+Thread.currentThread().getName());
for (Future<String> future : futures){
if (future.isDone()){
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
public static class $Call implements Callable<String>{
private int index ;
public $Call(int index){
this.index = index ;
}
@Override
public String call() throws Exception {
int first = 1 ;
int second = 1 ;
int third = 0 ;
while (true){
third = first+second ;
if (third > index){
break;
}
first = second ;
second = third ;
}
System.out.println("sub -- "+Thread.currentThread().getName());
return third+"" ;
}
}