Sun在java5中,对java线程的类库做了大量的扩展,其中线程池就是java5的新特征之一。除此之外,还有很多与多线程编程相关的内容。

  线程池的思想是一种对象池的思想,开放一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理。当有线程任务时,从池中取一个,执行完毕,对象归还给池。这样可以避免反复创建线程对象所带来的性能开销,节省了系统的资源。

  在Java5之前,要实现一个线程池是相当有难度的,现在Java5为我们做好了一切,我们只需要按照提供的API来使用,即可享受线程池带来的极大便利。

  Java5的线程池分好多种:固定尺寸的线程池、可变尺寸连接池等。

  在使用线程池之前,必须知道如何去创建一个线程池,在Java5中,需要了解的是java.util.concurrent.Executors类的API,这个类提供大量创建连接池的静态方法,是必须掌握的。

  一、固定大小的线程池

 

代码
 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 
 4 /**
 5  * 
 6  * @author hxm
 7  *
 8  */
 9 public class TestThread {
10     public static void main(String args[]){
11         //创建一个可重用固定线程数的线程池
12         ExecutorService pool = Executors.newFixedThreadPool(4);
13         
14         //创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程。
15 //        ExecutorService pool = Executors.newSingleThreadExecutor();
16         //创建实现了runnable接口的对象
17         Thread t1 = new MyThread();
18         Thread t2 = new MyThread();
19         Thread t3 = new MyThread();
20         Thread t4 = new MyThread();
21         Thread t5 = new MyThread();
22         //将线程放入池中进行执行
23         pool.execute(t1);
24         pool.execute(t2);
25         pool.execute(t3);
26         pool.execute(t4);
27         pool.execute(t5);
28         //关闭线程池
29         pool.shutdown();
30     }
31 }
32  class MyThread extends Thread{
33      
34      @Override
35      public void run(){
36          System.out.println(Thread.currentThread().getName()+" is running...");
37      }
38  }
39 
40 

 

 pool-1-thread-1 is running...
pool-1-thread-1 is running...
pool-1-thread-3 is running...
pool-1-thread-2 is running...
pool-1-thread-4 is running...

 

二、单任务线程池

 

代码
 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 
 4 /**
 5  * 
 6  * @author hxm
 7  *
 8  */
 9 public class TestThread {
10     public static void main(String args[]){
11         //创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程。
12         ExecutorService pool = Executors.newSingleThreadExecutor();
13         //创建实现了runnable接口的对象
14         Thread t1 = new MyThread();
15         Thread t2 = new MyThread();
16         Thread t3 = new MyThread();
17         Thread t4 = new MyThread();
18         Thread t5 = new MyThread();
19         //将线程放入池中进行执行
20         pool.execute(t1);
21         pool.execute(t2);
22         pool.execute(t3);
23         pool.execute(t4);
24         pool.execute(t5);
25         //关闭线程池
26         pool.shutdown();
27     }
28 }
29  class MyThread extends Thread{
30      
31      @Override
32      public void run(){
33          System.out.println(Thread.currentThread().getName()+" is running...");
34      }
35  }
36 
37 

 

 

pool-1-thread-1 is running...
pool-1-thread-1 is running...
pool-1-thread-1 is running...
pool-1-thread-1 is running...
pool-1-thread-1 is running...

 

对于以上两种连接池,大小都是固定的,当要加入的池的线程(或者任务)超过池最大尺寸时候,则进入此线程池需要排队等待。一旦池中有线程完毕,则排队等待的某个线程会入池执行。

 

三、 可变尺寸的线程池

代码
 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 
 4 /**
 5  * 
 6  * @author hxm
 7  *
 8  */
 9 public class TestThread {
10     public static void main(String args[]){
11         //创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。
12         ExecutorService pool = Executors.newCachedThreadPool();
13         //创建实现了runnable接口的对象
14         Thread t1 = new MyThread();
15         Thread t2 = new MyThread();
16         Thread t3 = new MyThread();
17         Thread t4 = new MyThread();
18         Thread t5 = new MyThread();
19         //将线程放入池中进行执行
20         pool.execute(t1);
21         pool.execute(t2);
22         pool.execute(t3);
23         pool.execute(t4);
24         pool.execute(t5);
25         //关闭线程池
26         pool.shutdown();
27     }
28 }
29  class MyThread extends Thread{
30      
31      @Override
32      public void run(){
33          System.out.println(Thread.currentThread().getName()+" is running...");
34      }
35  }

 

pool-1-thread-2 is running...
pool-1-thread-1 is running...
pool-1-thread-3 is running...
pool-1-thread-5 is running...
pool-1-thread-4 is running...

四、延迟连接池

 

代码
 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 import java.util.concurrent.ScheduledExecutorService;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 /**
 7  * 
 8  * @author hxm
 9  *
10  */
11 public class TestThread {
12     public static void main(String args[]){
13         //创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行
14         ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
15         //创建实现了runnable接口的对象
16         Thread t1 = new MyThread();
17         Thread t2 = new MyThread();
18         Thread t3 = new MyThread();
19         Thread t4 = new MyThread();
20         Thread t5 = new MyThread();
21         //将线程放入池中进行执行
22         pool.execute(t1);
23         pool.execute(t2);
24         pool.execute(t3);
25          //使用延迟执行风格的方法 
26         pool.schedule(t4, 10, TimeUnit.MILLISECONDS); 
27         pool.schedule(t5, 10, TimeUnit.MILLISECONDS); 
28 
29         //关闭线程池
30         pool.shutdown();
31     }
32 }
33  class MyThread extends Thread{
34      
35      @Override
36      public void run(){
37          System.out.println(Thread.currentThread().getName()+" is running...");
38      }
39  }
40 
41 

pool-1-thread-2 is running...
pool-1-thread-2 is running...
pool-1-thread-1 is running...
pool-1-thread-2 is running...
pool-1-thread-1 is running...

五、单任务延迟连接池

代码
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 
 * 
@author hxm
 *
 
*/
public class TestThread {
    
public static void main(String args[]){
        
//创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行
        ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
        
//创建实现了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.schedule(t4, 10, TimeUnit.MILLISECONDS); 
        pool.schedule(t5, 
10, TimeUnit.MILLISECONDS); 

        
//关闭线程池
        pool.shutdown();
    }
}
 
class MyThread extends Thread{
     
     @Override
     
public void run(){
         System.out.println(Thread.currentThread().getName()
+" is running...");
     }
 }

 

pool-1-thread-1 is running...
pool-1-thread-1 is running...
pool-1-thread-1 is running...
pool-1-thread-1 is running...
pool-1-thread-1 is running...

六、自定义线程池

代码
 1 import java.util.concurrent.ArrayBlockingQueue;
 2 import java.util.concurrent.BlockingQueue;
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.ScheduledExecutorService;
 6 import java.util.concurrent.ThreadPoolExecutor;
 7 import java.util.concurrent.TimeUnit;
 8 
 9 /**
10  * 
11  * @author hxm
12  *
13  */
14 public class TestThread {
15     public static void main(String args[]){
16           //创建等待队列 
17         BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(20); 
18         //创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。 
19         ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,2,TimeUnit.MILLISECONDS,bqueue); 
20 
21         //创建实现了runnable接口的对象
22         Thread t1 = new MyThread();
23         Thread t2 = new MyThread();
24         Thread t3 = new MyThread();
25         Thread t4 = new MyThread();
26         Thread t5 = new MyThread();
27         //将线程放入池中进行执行
28         pool.execute(t1);
29         pool.execute(t2);
30         pool.execute(t3);
31         pool.execute(t4);
32         pool.execute(t5);
33         //关闭线程池
34         pool.shutdown();
35     }
36 }
37  class MyThread extends Thread{
38      
39      @Override
40      public void run(){
41          System.out.println(Thread.currentThread().getName()+" is running...");
42     try { 
43          Thread.sleep(100L); 
44      } catch (InterruptedException e) { 
45              e.printStackTrace(); 
46      } 
47      }
48  }
49 
50 

 

pool-1-thread-2 is running...
pool-1-thread-1 is running...
pool-1-thread-2 is running...
pool-1-thread-1 is running...
pool-1-thread-2 is running...

 

里面的内容还有待在实际使用中更深入的理解消化!!!!