ExecutorService

public class PoolDemo {
    public static void main(String[] args) {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        for (int threadNumber = 0; threadNumber < 100; threadNumber ++) {
            createPushThread(fixedThreadPool);
        }
        try {
            fixedThreadPool.shutdown(); 
            fixedThreadPool.awaitTermination(1, TimeUnit.NANOSECONDS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    private static void createPushThread(ExecutorService fixedThreadPool) {
        fixedThreadPool.execute(new Runnable() { 
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("do sth...");
                } catch (Exception e) {
                }
            }
        });
    }
}

你可能感兴趣的:(ExecutorService)