线程池可以看作一个令牌库。如果要运行,就需要获取一个令牌,运行完毕退回令牌。如果令牌发光了,就禁止新的运行,等待退回的令牌。
java.util.concurrent.Executors里有很多方法创建固定个数的线程池还有java.util.concurrent.ThreadPoolExecutor等
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 10, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy())