ExecutorService pool = Executors.newCachedThreadPool();//使用newCachedThreadPool();創建靜態線程池 for (int j = 1; j <= 10; j++) { pool.execute(new MyThread(j)); try { /* * 線程出現休眠時,只有一個線程在執行 * 線程沒有休眠時,就會出現多個線程並發執行 */ Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class MyThread implements Runnable {//創建一個類實現Runnable接口 int num;//計數 public MyThread(){ } public MyThread(int num) { this.num = num; } @Override public void run() { System.out.println(Thread.currentThread().getName()+":"+num); }