import java.util.concurrent.Callable;
public class Main {
public static void main(String[] args) throws Exception {
ThreadPool pool = new ThreadPool(4);
for (int i = 0; i < 10; i++) {
final int num = i;
pool.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Object result = num * 2;
System.out.println("Task " + num + " result: " + result);
return result;
}
});
}
pool.start();
pool.shutdown();
Object[] results = pool.getResults();
System.out.print("All results: ");
for (Object result : results) {
System.out.print(result + " ");
}
}
}
网友评论