import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPool {
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newCachedThreadPool();
for(int i = 1; i <= 10; i++){
pool.execute(new Runnable() {
public void run() {
System.out.println("thread name: " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
Thread.sleep(10000);
System.out.println("----------------------------------");
for(int i = 1; i <= 15; i++){
pool.execute(new Runnable() {
public void run() {
System.out.println("thread name: " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
pool.shutdown();
}
}
pool也有 pool.submit(new callable(){
})
这种方式启动线程后有返回值
判断返回是否成功
f.isDone
获取返回值
f.get()