Java 線程池的實現

來源:互聯網
上載者:User

標籤:zed   比較   adl   ted   this   []   lin   new   pack   

線程池的實現   組成

    一個比較簡單的線程池至少應包括

        線程池管理器:建立、銷毀並管理線程池,將背景工作執行緒放入線程池中;

        背景工作執行緒:一個可以迴圈執行任務的線程,在沒有任務時進行等待;

        任務隊列:提供一種緩衝機制,將沒有處理的任務放在任務隊列中;

        任務介面:每個任務必須實現的介面,主要用來規定任務的入口、任務執行完後的收尾工作、任務的執行狀態等。 背景工作執行緒通過該介面調度任務的執行。

   原理

   類似於作業系統中的緩衝區,流程如下:

   先啟動若干數量的線程,並讓這些線程都處於等待狀態,當用戶端有一個新請求時,就會喚醒線程池中的某一個等待線程,讓他來處理用戶端的這個請求,當處理完後,繼續處於等待狀態。

代碼實現

   

package thread;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.*;public class threadpool {            static class ThreadPoolManager{            private int MaxThread;            private Vector ThreadQueue;                        public void setMaxThread(int count){                this.MaxThread=count;            }                        public ThreadPoolManager(int count){                setMaxThread(count);                System.out.println("Starting Thread Pool");                ThreadQueue=new Vector();                for(int i=0;i<=MaxThread;i++){                    SimpleThread thread=new SimpleThread(i);                    ThreadQueue.addElement(thread);                    thread.start();                }            }                        public void process(String argument){                int i;                for(i=0;i<ThreadQueue.size();i++){                    SimpleThread current=(SimpleThread) ThreadQueue.elementAt(i);                    if(!current.isRunning()){                        System.out.println("Thread"+(i+1)+"is processing:"+argument);                        current.setArgument(argument);                        current.setRunning(true);                        return;                    }                }                                if(i==ThreadQueue.size()){                    System.out.println("pool is full,try in anthor time");                }            }                }            static class SimpleThread extends Thread{        private boolean RunningFlag;        private String argument;        public boolean isRunning(){            return RunningFlag;        }                public synchronized void setRunning(Boolean Flag){            RunningFlag=Flag;            if(Flag){                this.notify();            }        }                public String getArgument(){            return this.argument;        }                public void setArgument(String string){            argument=string;        }                public SimpleThread(int threadNumber){            RunningFlag=false;            System.out.println("thread"+threadNumber+"started.");        }                public synchronized void run(){            try {                while(true){                    if(!RunningFlag){                        this.wait();                    }                    else{                        System.out.println("process"+getArgument()+"……done!");                        sleep(5000);                        System.out.println("Thread is sleeping.");                        setRunning(false);                                            }                }            } catch (Exception e) {                // TODO: handle exception                System.out.println("Interrupt");            }        }    }        public static void main(String[] args) {        try {            BufferedReader brBufferedReader=new BufferedReader(new InputStreamReader(System.in));            String string;            ThreadPoolManager manager=new ThreadPoolManager(10);            while((string=brBufferedReader.readLine())!=null){                manager.process(string);            }        } catch (Exception e) {            // TODO: handle exception        }    }        }

 

   

   

 

Java 線程池的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.