现在的位置: 首页 > 综合 > 正文

创建java线程池

2018年04月18日 ⁄ 综合 ⁄ 共 5048字 ⁄ 字号 评论关闭

线程池的作用:

     线程池作用就是限制系统中执行线程的数量。
     根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池中有等待的工作线程,就可以开始运行了;否则进入等待队列。

 

为什么要用线程池:

  1. 减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务
  2. 可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1MB内存,线程开的越多,消耗的内存也就越大,最后死机)

线程池类

    

Java代码  收藏代码
  1. package com.tdt.impl.ls;  
  2.   
  3. import java.util.LinkedList;  
  4.   
  5. /** 
  6.  * @project LocationGateway 
  7.  * @author sunnylocus    
  8.  * @verson 1.0.0 
  9.  * @date   Aug 2, 2008 
  10.  * @jdk    1.4.2 
  11.  */  
  12. public class ThreadPool extends ThreadGroup {  
  13.     private boolean isClosed = false;  //线程池是否关闭   
  14.     private LinkedList workQueue;      //工作队列  
  15.     private static int threadPoolID = 1;  //线程池的id  
  16.     public ThreadPool(int poolSize) {  //poolSize 表示线程池中的工作线程的数量  
  17.   
  18.         super(threadPoolID + "");      //指定ThreadGroup的名称  
  19.         setDaemon(true);               //继承到的方法,设置是否守护线程池  
  20.         workQueue = new LinkedList();  //创建工作队列  
  21.         for(int i = 0; i < poolSize; i++) {  
  22.             new WorkThread(i).start();   //创建并启动工作线程,线程池数量是多少就创建多少个工作线程  
  23.         }  
  24.     }  
  25.       
  26.     /** 向工作队列中加入一个新任务,由工作线程去执行该任务*/  
  27.     public synchronized void execute(Runnable task) {  
  28.         if(isClosed) {  
  29.             throw new IllegalStateException();  
  30.         }  
  31.         if(task != null) {  
  32.             workQueue.add(task);//向队列中加入一个任务  
  33.             notify();           //唤醒一个正在getTask()方法中待任务的工作线程  
  34.         }  
  35.     }  
  36.       
  37.     /** 从工作队列中取出一个任务,工作线程会调用此方法*/  
  38.     private synchronized Runnable getTask(int threadid) throws InterruptedException {  
  39.         while(workQueue.size() == 0) {  
  40.             if(isClosed) return null;  
  41.             System.out.println("工作线程"+threadid+"等待任务...");  
  42.             wait();             //如果工作队列中没有任务,就等待任务  
  43.         }  
  44.         System.out.println("工作线程"+threadid+"开始执行任务...");  
  45.         return (Runnable) workQueue.removeFirst(); //反回队列中第一个元素,并从队列中删除  
  46.     }  
  47.       
  48.     /** 关闭线程池 */  
  49.     public synchronized void closePool() {  
  50.         if(! isClosed) {  
  51.             waitFinish();        //等待工作线程执行完毕  
  52.             isClosed = true;  
  53.             workQueue.clear();  //清空工作队列  
  54.             interrupt();        //中断线程池中的所有的工作线程,此方法继承自ThreadGroup类  
  55.         }  
  56.     }  
  57.       
  58.     /** 等待工作线程把所有任务执行完毕*/  
  59.     public void waitFinish() {  
  60.         synchronized (this) {  
  61.             isClosed = true;  
  62.             notifyAll();            //唤醒所有还在getTask()方法中等待任务的工作线程  
  63.         }  
  64.         Thread[] threads = new Thread[activeCount()]; //activeCount() 返回该线程组中活动线程的估计值。  
  65.         int count = enumerate(threads); //enumerate()方法继承自ThreadGroup类,根据活动线程的估计值获得线程组中当前所有活动的工作线程  
  66.         for(int i =0; i < count; i++) { //等待所有工作线程结束  
  67.             try {  
  68.                 threads[i].join();  //等待工作线程结束  
  69.             }catch(InterruptedException ex) {  
  70.                 ex.printStackTrace();  
  71.             }  
  72.         }  
  73.     }  
  74.   
  75.     /** 
  76.      * 内部类,工作线程,负责从工作队列中取出任务,并执行 
  77.      * @author sunnylocus 
  78.      */  
  79.     private class WorkThread extends Thread {  
  80.         private int id;  
  81.         public WorkThread(int id) {  
  82.             //父类构造方法,将线程加入到当前ThreadPool线程组中  
  83.             super(ThreadPool.this,id+"");  
  84.             this.id =id;  
  85.         }  
  86.         public void run() {  
  87.             while(! isInterrupted()) {  //isInterrupted()方法继承自Thread类,判断线程是否被中断  
  88.                 Runnable task = null;  
  89.                 try {  
  90.                     task = getTask(id);     //取出任务  
  91.                 }catch(InterruptedException ex) {  
  92.                     ex.printStackTrace();  
  93.                 }  
  94.                 //如果getTask()返回null或者线程执行getTask()时被中断,则结束此线程  
  95.                 if(task == nullreturn;  
  96.                   
  97.                 try {  
  98.                     task.run();  //运行任务  
  99.                 }catch(Throwable t) {  
  100.                     t.printStackTrace();  
  101.                 }  
  102.             }//  end while  
  103.         }//  end run  
  104.     }// end workThread  
  105. }  

 

2.测试类

  

Java代码  收藏代码
  1. package com.tdt.test;  
  2.   
  3. import com.tdt.impl.ls.ThreadPool;  
  4.   
  5. public class ThreadPoolTest {  
  6.       
  7.     public static void main(String[] args) throws InterruptedException {  
  8.         ThreadPool threadPool = new ThreadPool(3); //创建一个有个3工作线程的线程池  
  9.         Thread.sleep(500); //休眠500毫秒,以便让线程池中的工作线程全部运行  
  10.         //运行任务  
  11.         for (int i = 0; i <=5 ; i++) { //创建6个任务  
  12.             threadPool.execute(createTask(i));  
  13.         }  
  14.         threadPool.waitFinish(); //等待所有任务执行完毕  
  15.         threadPool.closePool(); //关闭线程池  
  16.   
  17.     }  
  18.   
  19.     private static Runnable createTask(final int taskID) {  
  20.         return new Runnable() {  
  21.             public void run() {  
  22.             //  System.out.println("Task" + taskID + "开始");  
  23.                 System.out.println("Hello world");  
  24.             //  System.out.println("Task" + taskID + "结束");  
  25.             }  
  26.         };  
  27.     }  
  28. }  

 

 

结果:

Java代码  收藏代码
  1. 工作线程0等待任务...  
  2. 工作线程1等待任务...  
  3. 工作线程2等待任务...  
  4.   
  5. 工作线程0开始执行任务...  
  6. Hello world  
  7. 工作线程0等待任务...  
  8.   
  9. 工作线程1开始执行任务...  
  10. Hello world  
  11. 工作线程1等待任务...  
  12.   
  13. 工作线程2开始执行任务...  
  14. Hello world  
  15. 工作线程2等待任务...  
  16.   
  17. 工作线程0开始执行任务...  
  18. Hello world  
  19. 工作线程0等待任务...  
  20.   
  21. 工作线程1开始执行任务...  
  22. Hello world  
  23. 工作线程1等待任务...  
  24.   
  25. 工作线程2开始执行任务...  
  26. Hello world  
  27. 工作线程2等待任务... 

抱歉!评论已关闭.