实例代码
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
for(int i =0 ; i < 20; i++){
executor.submit(new SubThread(i * 1000));
}
executor.shutdown();
try {//等待直到所有任务完成
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行完成......");
}
}
class SubThread implements Callable<String>{
private long ms;
public SubThread(long ms){
this.ms = ms;
}
public String call() throws Exception {
Thread.sleep(ms);
System.out.println("等待了 --"+ ms+ "ms");
return null;
}
}