I want to get Future
return value and use it like variable.
I have this Future
function
Future _fetchUserInfo(String id)
import java.util.ArrayList;
import java.util.List;
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;
class ThreadExample implements Callable{
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
return "Ashish";
}
}
public class FutureThreadExample {
public static void main(String a[]) throws InterruptedException, ExecutionException {
ExecutorService executorService=Executors.newFixedThreadPool(1);
List >objList=new ArrayList>();
for(int i=0;i<10;i++) {
Future obj=executorService.submit(new ThreadExample());
objList.add(obj);
}
for( Future fut:objList) {
System.out.println(fut.get());
}
executorService.shutdown();
}
}