1. 程式人生 > >JAVA執行緒池的簡單實現及優先順序設定

JAVA執行緒池的簡單實現及優先順序設定

{

    
privatestatic ThreadPool instance =null;

    
// 優先順序低
publicstaticfinalint PRIORITY_LOW =0;

    
// 普通
publicstaticfinalint PRIORITY_NORMAL =1;

    
// 高
publicstaticfinalint PRIORITY_HIGH =2;

    
// 用以儲存空閒連線
private List[] _idxThreads;

    
// 關閉
privateboolean _shutdown =false;

    
// 執行緒數量
privateint _threadCount =0;

    
// debug資訊是否輸出
privateboolean _debug =false;

    
/**
     * 返回ThreadPool例項
     * 
     * 
@return
     
*/

    
publicstatic 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
     
*/

    
protectedsynchronizedvoid 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:
                
thrownew IllegalStateException("沒有此種級別");
            }

            
// 通知所有執行緒
            notifyAll();

        }
else{
            
if (this._debug) {
                System.out.println(
"ThreadPool.repool():登出中……");
            }

            repool.shutDown();
        }

        
if(this._debug){
            System.out.println(
"ThreadPool.repool():完成");
        }

    }

    
publicvoid setDebug(boolean debug){
        
this._debug=debug;
    }

    
publicsynchronizedvoid 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
     
*/

    
publicsynchronizedvoid start(Runnable target,int priority){
        Pooled thread
=null;
        List idleList
=this._idxThreads[priority];
        
int idleSize=idleList.size();

        
if(idleSize>0){
            
int lastIndex=idleSize-1;
            thread
=(Pooled)idleList.get(lastIndex);
            idleList.remove(idleList);
            thread.setTarget(target);
        }
else{
            
this._threadCount++;
            thread
=new Pooled(target,"Pooled->"+this._threadCount,this);
            
switch(priority){
            
            
case PRIORITY_LOW:
                thread.setPriority(Thread.MIN_PRIORITY);
                
break;
            
case PRIORITY_NORMAL:
                thread.setPriority(Thread.NORM_PRIORITY);
                
break;
            
case PRIORITY_HIGH:
                thread.setPriority(Thread.MAX_PRIORITY);
                
break;
                
default:
                    thread.setPriority(Thread.NORM_PRIORITY);
            }

            
//啟動
            thread.start();
    
            }

        
        
    }


    
/**
     * 返回執行緒數量
     * 
     * 
@return
     
*/

    
publicint getThreadsCount() {
        
returnthis._threadCount;
    }


}