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执行中。。。