package com.test;
2.
3.import java.util.ArrayList;
4.import java.util.List;
5.import java.util.concurrent.Callable;
6.import java.util.concurrent.ExecutionException;
7.import java.util.concurrent.ExecutorService;
8.import java.util.concurrent.Executors;
9.import java.util.concurrent.Future;
10.
11.public class CallableTest {
12. public static void main(String[] args) {
13. ExecutorService exec=Executors.newCachedThreadPool();
14. List<Future<String>> results=new ArrayList<Future<String>>();
15.
16. for(int i=0;i<5;i++) {
17. results.add(exec.submit(new TaskWithResult(i)));
18. }
19.
20. for(Future<String> fs :results) {
21. try {
22. System.out.println(fs.get());
23. } catch (InterruptedException e) {
24. e.printStackTrace();
25. } catch (ExecutionException e) {
26. e.printStackTrace();
27. }
28. }
29. }
30.}
31.
32.class TaskWithResult implements Callable<String> {
33. private int id;
34. public TaskWithResult(int id) {
35. this.id=id;
36. }
37.
38. @Override
39. public String call() throws Exception {
40. return "result of TaskWithResult "+id;
41. }
42.}
package com.test;
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;
public class CallableTest {
public static void main(String[] args) {
ExecutorService exec=Executors.newCachedThreadPool();
List<Future<String>> results=new ArrayList<Future<String>>();
for(int i=0;i<5;i++) {
results.add(exec.submit(new TaskWithResult(i)));
}
for(Future<String> fs :results) {
try {
System.out.println(fs.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
class TaskWithResult implements Callable<String> {
private int id;
public TaskWithResult(int id) {
this.id=id;
}
@Override
public String call() throws Exception {
return "result of TaskWithResult "+id;
}
}