JAVA線程池的簡單實現及優先級設置 – JAVA編程語言程序開發技術文章

我們大傢都知道,在處理多線程服務並發時,由於創建線程需要占用很多的系統資源,所以為瞭避免這些不必要的損耗,通常我們采用線程池來解決這些問題。
  線程池的基本原理是,首先創建並保持一定數量的線程,當需要使用線程時,我們從池中取得線程,再將需要運行的任務交給線程進行處理,當任務完成後再將其釋放回池中。
 下面,我給出一個很簡單的實現模型,僅供參考。


 ThreadPool.java package org.loon.framework.util.test;


import java.util.LinkedList;
import java.util.List;


/** *//**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 *
 * @author chenpeng
 * @email:ceponline@yahoo.com.cn
 * @version 0.1
 */
public class ThreadPool …{


    private static ThreadPool instance = null;


    // 優先級低
    public static final int PRIORITY_LOW = 0;


    // 普通
    public static final int PRIORITY_NORMAL = 1;


    // 高
    public static final int PRIORITY_HIGH = 2;


    // 用以保存空閑連接
    private List[] _idxThreads;


    // 關閉
    private boolean _shutdown = false;


    // 線程數量
    private int _threadCount = 0;


    // debug信息是否輸出
    private boolean _debug = false;


    /** *//**
     * 返回ThreadPool實例
     *
     * @return
     */
    public static ThreadPool getInstance() …{
        if (instance == null) …{
                    instance = new ThreadPool();
            }
        return instance;
    }


    // 初始化線程list
    private ThreadPool() …{
        this._idxThreads = new List[] …{ new LinkedList(), new LinkedList(),
                new LinkedList() };
        this._threadCount=0;
    }


    /** *//**
     * 同步方法,完成任務後將資源放回線程池中
     * @param repool
     */
    protected synchronized void repool(Pooled repool) …{
        if (this._shutdown) …{
            if (this._debug) …{
                System.out.println(“ThreadPool.repool():重設中……”);
            }
            // 優先級別判定
            switch (repool.getPriority()) …{
            case Thread.MIN_PRIORITY:
                this._idxThreads[PRIORITY_LOW].add(repool);
                break;
            case Thread.NORM_PRIORITY:
                this._idxThreads[PRIORITY_NORMAL].add(repool);
                break;
            case Thread.MAX_PRIORITY:
                this._idxThreads[PRIORITY_HIGH].add(repool);
                break;
            default:
                throw new IllegalStateException(“沒有此種級別”);
            }
            // 通知所有線程
            notifyAll();


        } else …{
            if (this._debug) …{
                System.out.println(“ThreadPool.repool():註銷中……”);
            }
            repool.shutDown();
        }
        if(this._debug)…{
            System.out.println(“ThreadPool.repool():完成”);
        }
    }
    public void setDebug(boolean debug)…{
        this._debug=debug;
    }
    public synchronized  void shutDown()…{
        this._shutdown=true;
        if(this._debug)…{
            System.out.println(“ThreadPool.shutDown():關閉中……”);
        }
        for(int index=0;index<=PRIORITY_NORMAL;index++)…{
            List threads=this._idxThreads[index];
            for(int threadIndex=0;threadIndex<threads.size();threadIndex++)…{
                Pooled idleThread=(Pooled)threads.get(threadIndex);
                idleThread.shutDown();
            }
        }
        notifyAll();
    }
   
    /** *//**
     * 以指定的優先級啟動線程
     * @param target
     * @param priority
     */
    public synchronized void start(Runnable target,int priority)…{
        Pooled thread=null;
        List idleList=this._idxThreads[priority];
        int idleSize=idleList.size();


        if(idleSize>0)…{
            int lastIndex=idleSize-1;
         &n

發佈留言