publicclassMyThread extends Thread { @Override publicvoid run() { System.out.println(Thread.currentThread().getName() + "執行中。。。"); } } ①newSingleThreadExecutor publicclassTestSingleThreadExecutor { publicstaticvoid main(String[] args) { //建立一個可重用固定線程數的線程池 ExecutorService pool = Executors. newSingleThreadExecutor(); //建立實現了Runnable接口對象 Thread tt1 = new MyThread(); Thread tt2 = new MyThread(); Thread tt3 = new MyThread(); Thread tt4 = new MyThread(); Thread tt5 = new MyThread(); //將線程放入池中並執行 pool.execute(tt1); pool.execute(tt2); pool.execute(tt3); pool.execute(tt4); pool.execute(tt5); //關閉 pool.shutdown(); } } result: pool-1-thread-1執行中。。。 pool-1-thread-1執行中。。。 pool-1-thread-1執行中。。。 pool-1-thread-1執行中。。。 pool-1-thread-1執行中。。。 ②newFixedThreadExecutor(n) publicclass TestFixedThreadPool { publicstaticvoid main(String[] args) { //建立一個可重用固定線程數的線程池 ExecutorService pool = Executors.newFixedThreadPool(2); //建立實現了Runnable接口對象 Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); Thread t4 = new MyThread(); Thread t5 = new MyThread(); //將線程放入池中進行執行 pool.execute(t1); pool.execute(t2); pool.execute(t3); pool.execute(t4); pool.execute(t5); //關閉線程池 pool.shutdown(); } } result: pool-1-thread-1執行中。。。 pool-1-thread-2執行中。。。 pool-1-thread-1執行中。。。 pool-1-thread-2執行中。。。 pool-1-thread-1執行中。。。 ③newCacheThreadExecutor publicclass TestCachedThreadPool { publicstaticvoid main(String[] args) { //建立一個可重用固定線程數的線程池 ExecutorService pool = Executors.newCachedThreadPool(); //建立實現了Runnable接口對象 Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); Thread t4 = new MyThread(); Thread t5 = new MyThread(); //將線程放入池中進行執行 pool.execute(t1); pool.execute(t2); pool.execute(t3); pool.execute(t4); pool.execute(t5); //關閉線程池 pool.shutdown(); } } result: pool-1-thread-1執行中。。。 pool-1-thread-2執行中。。。 pool-1-thread-4執行中。。。 pool-1-thread-3執行中。。。 pool-1-thread-5執行中。。。