????????
?????????ThreadPoolExecutor???????????Executors??????????????????????????????????????С?????忴??????????

 

public class TestThread {
/**
* ?????????????????????????
* ??????????????????ζ?????????
* Executors.newCachedThreadPool()???÷????????????????????????????????1??????????
* ???????Executors.newFixedThreadPool(n)
*
* ?????????????????????SecheduledThreadPoolExecutor
*/
public static void main(String[] args) throws InterruptedException?? ExecutionException {
int nThreads = 5;
/**
* Executors??ThreadPoolExecutor???????????
*/
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
//submit?з????????execute??з???????з????????Exception?????
Future res = executor.submit(new ConsumerThread());
//executor.execute(new ConsumerThread());
/**
* shutdown???ú?????????submit?μ?task?????submit??????????
* shutdownNow????????????е?task??????????δ??е?task??list
*/
executor.shutdown();
//???shutdown????shutdown????????е??????????????????????????????к???????
executor.awaitTermination(1?? TimeUnit.DAYS);
//?????н???????????????????????????execute????????????????????????????submit??????????res.get()????????????????????
System.out.println("future result:"+res.get());
}
static class ConsumerThread implements Runnable{
@Override
public void run() {
for(int i=0;i<5;i++) {
System.out.println(i);
}
}
}
}

 

?????????
????0
????1
????2
????3
????4
????future result:null