• java線程池舉例講解

    6
    Java Apache 數據庫 C/C++ 17802 次瀏覽
    在CPU和內存還有磁盤IO并沒有充分利用的時候,就可以在重構的時候,使用線程操作,將過去需要占用一定時間的同步操作,進行并發處理。這樣,創建多個線程來解決同一個問題。這樣就可以使操作成為異步,不需要進行等待就可以同時操作很多事情。
    那么線程需要創建多少呢?
    每監聽到一個請求就創建一個線程,然后操作之后就銷毀。這種辦法可行么?

    如果突然在客戶端提交了1000000 * N 個請求,這些請求都進入服務器,然后在這里,為每一個請求都創建一個線程來執行。這樣程序就會失去控制。最終擠爆服務器CPU和內存,使程序崩潰。

    怎么樣合理、可控的處理操作,變成了重點。

    線程池,顧名思義,在池化開發的現在,如數據庫連接池這種開發模式已經廣泛流行。
    在池中創建連接實例,在訪問數據庫時,從連接池中取得連接,操作之后歸還。這樣就可以有效的限制資源數量,但是又能提高操作速度。
    package test.dao;
    
    import java.util.LinkedList;
    
    import org.apache.log4j.Logger;
    
    
    /**
    * 
    * @author Andy.xiaomeng
    *
    */
    public class ThreadPool {
    
            private static final Logger Log = Logger.getLogger(ThreadPool.class);
            
            private ThreadWorker[] worker;
            private LinkedList<Runnable> queue;
            private int poolSize;
            
            public int getPoolSize(){
                    return poolSize;
            }
            
            //initialize thread pool with pool size
            public ThreadPool(int size){
                    
                    poolSize = size;
                    worker = new ThreadWorker[size];
                    queue = new LinkedList<Runnable>();
                    
                    for (int i = 0; i < size; i++) {
                            worker[i] = new ThreadWorker(); 
                            worker[i].start();
                    }
                    
            }
            
            
            
            //add work queue
            public void addQueue(Runnable runnable){
                    synchronized(queue) {
                            queue.add(runnable);
                            queue.notify();
                    }
            }
            
            //worker do work
            private class ThreadWorker extends Thread{
                    
                    public void run(){
                            
                            //runnable
                            Runnable runable;
                            
                            while (true) {
                                    
                                    synchronized(queue) {
                        while (queue.isEmpty()) {
                            try
                            {
                                    //is work queue is empty, wait
                                queue.wait();
                            }
                            catch (Exception e)
                            {
                                    Log.error("pool queue wait", e);
                            }
                        }
                        
                        //get work in queue, and remove it 
                        runable = queue.removeFirst();
                    }
    
                    try {
                            //do work
                            runable.run();
                    }
                    catch (RuntimeException e) {
                            Log.error("unknow runtime exception", e);
                    }
                                    
                            }
                    }
                    
            }
            
            public static void main(String[] args) {
                    
                    //test
                    ThreadPool pool = new ThreadPool(111);
                    
                    
                    //
                    class SSS implements Runnable{
                            private String abc;
                            public void setAbc(String aaaaa){
                                    this.abc = aaaaa;
                            }
                            
                            public void run() {
                                    System.out.println("do runnable " + abc);
                            }
                    }
                    
                    for (int i = 0; i < 200; i++) {
                            SSS a = new SSS();
                            a.setAbc(String.valueOf(i + 1));
                            pool.addQueue(a);
                    }
                    
            }
    }

    相似問題

    相關經驗

    相關資訊

    相關文檔

  • sesese色