1. package com.thread.app01; 
  2.  
  3. import java.util.concurrent.ExecutorService; 
  4. import java.util.concurrent.Executors; 
  5.  
  6. public class App01 implements Runnable { 
  7.     int count = 100
  8.  
  9.     @Override 
  10.     public void run() { 
  11.         this.Ticketing(); 
  12.     } 
  13.  
  14.     public void Ticketing() { 
  15.         while (count != 0) { 
  16.             System.out.println(Thread.currentThread().getName() + " " + "当前" 
  17.                     + count--); 
  18.         } 
  19.     } 
  20.  
  21.     /** 
  22.      * @param args 
  23.      */ 
  24.     public static void main(String[] args) { 
  25.         App01 app01 = new App01(); 
  26.  
  27.         /** 
  28.          * <b>Executors.newCachedThreadPool()</b> 
  29.          * <p> 
  30.          * 创建一个可根据需要创建新线程的线程池,但是在以前构 造的线程可用时将重用它们,并在需要的时使用提供的TherdFacory创建线程 
  31.          * </p> 
  32.          * <p> 
  33.          * 在程序执行过程中通常会创建与所需数量相同的线程,然后在它回收旧线程时停止创建新线程 
  34.          * </p> 
  35.          */ 
  36.  
  37.         ExecutorService exec = Executors.newCachedThreadPool(); 
  38.         for (int i = 0; i < 4; i++) { 
  39.             exec.execute(app01); 
  40.         } 
  41.         exec.shutdown(); 
  42.  
  43.         /** 
  44.          * <b>Executors.newFixedThreadPool(int count);</b> 
  45.          * <p> 
  46.          * 创建一个可重用固定线程的线程池,以共享的×××队列方式运行这些线程 
  47.          * </p> 
  48.          * <p> 
  49.          * 预先执行代价高昂的线程分配,因而也就可以跟制线程的数量 
  50.          * <p> 
  51.          */ 
  52.  
  53.         ExecutorService exec1 = Executors.newFixedThreadPool(5); 
  54.         for (int i = 0; i < 4; i++) { 
  55.             exec1.execute(app01); 
  56.         } 
  57.         exec1.shutdown(); 
  58.  
  59.         /** 
  60.          * <b>Executors.newSingleThreadExecutor()</b> 
  61.          * <p> 
  62.          * 创建一个单用单个worker线程的Executor,以×××队列方式运行 
  63.          * <p> 
  64.          * <p> 
  65.          * 这种方式更好的解决资源上同步,省去只是为了维持某些事物的原型而进行各种协调 
  66.          * <P> 
  67.          */ 
  68.  
  69.         ExecutorService exec2 = Executors.newSingleThreadExecutor(); 
  70.         for (int i = 0; i < 4; i++) { 
  71.             exec2.execute(app01); 
  72.         } 
  73.         exec2.shutdown(); 
  74.  
  75.     } 
  76.