아날로그 스레드 풀

4262 단어 스레드 풀
예:
public class ThreadPool extends ThreadGroup {    
    private boolean isClosed = false;  //            
    private LinkedList workQueue;      //        
    private static int threadPoolID = 1;  //    id    
		
    public ThreadPool(int poolSize) {  //poolSize                   
    
        super(threadPoolID + "");      //  ThreadGroup       
        setDaemon(true);               //      ,             
        workQueue = new LinkedList();  //          
        for(int i = 0; i < poolSize; i++) {    
            new WorkThread(i).start();   //         ,                      
        }    
    }    
	
        
    /**              ,           */    
    public synchronized void execute(Runnable task) {    
        if(isClosed) {    
            throw new IllegalStateException();    
        }    
        if(task != null) {    
            workQueue.add(task);//              
            notify();           //      getTask()               
        }    
    }    
     
        
    /**       */    
    public synchronized void closePool() {    
        if(! isClosed) {    
            waitFinish();        //              
            isClosed = true;    
            workQueue.clear();  //          
            interrupt();        //              ,      ThreadGroup     
        }    
    }    
        
    /**                */    
    public void waitFinish() {    
        synchronized (this) {    
            isClosed = true;    
            notifyAll();            //      getTask()                
        }    
        Thread[] threads = new Thread[activeCount()];   
        int count = enumerate(threads);   
        for(int i =0; i < count; i++) { //              
            try {    
                threads[i].join();  //            
            }catch(InterruptedException ex) {    
                ex.printStackTrace();    
            }    
        }    
    }    
	
	
	
	
	/**             ,          */    
    private synchronized Runnable getTask(int threadid) throws InterruptedException {    
        while(workQueue.size() == 0) {    
            if(isClosed) return null;    
            System.out.println("    "+threadid+"    ...");    
            wait();             //           ,         
        }    
        System.out.println("    "+threadid+"      ...");    
        return (Runnable) workQueue.removeFirst(); //          ,           
    }
    
    /**  
     *    ,    ,            ,     
     * @author sunnylocus  
     */    
    private class WorkThread extends Thread {    
        private int id;    
		
        public WorkThread(int id) {    
            //      ,        ThreadPool        
            super(ThreadPool.this,id+"");    
            this.id =id;    
        }    
		
        public void run() {    
            while(! isInterrupted()) {  //isInterrupted()     Thread ,             
                Runnable task = null;    
                try {    
                    task = getTask(id);     //        
                }catch(InterruptedException ex) {    
                    ex.printStackTrace();    
                }    
                //  getTask()  null      getTask()    ,          
                if(task == null) return;    
                    
                try {    
                    task.run();  //        
                }catch(Throwable t) {    
                    t.printStackTrace();    
                }    
            }   
        }
		
    } 
} 

 
참고 사항:
1. 스레드 탱크를 구축할 때 N개의 스레드를 구축하고 가동한다
2. 스레드 탱크에 작업 추가 즉 스레드 탱크의 대기 대기열에 작업 추가, 추가 후 스레드 실행 깨우기
3. 각 스레드는 대기 대기열에서 임무를 취하고 실행한다.
 
 
 
 
 

좋은 웹페이지 즐겨찾기