《JAVA 性能優(yōu)化》上面的源碼-簡(jiǎn)單線程池的實(shí)現(xiàn)-待優(yōu)化

package com.java.reflection;

import java.util.List;
import java.util.Vector;

/**
 * @author shujun.xiong
 * @date 2019/7/14
 */
public class SimpleThreadPool {

    private static SimpleThreadPool instance = null;
    private Vector<PThread> idleThread;
    private int threadCounter;
    private boolean isShutDown = false;


    private  SimpleThreadPool()
    {
        this.idleThread = new Vector(5);
        this.threadCounter = 0;
    }

    public  int getThreadCounter()
    {
        return this.threadCounter;
    }


    public  synchronized static SimpleThreadPool getInstance()
    {
        if(instance == null)
        {
            return new SimpleThreadPool();
        }
        return instance;
    }

    protected synchronized void repool(PThread pThread)
    {
        if(!isShutDown)
        {
            idleThread.add(pThread);
        }
        else
        {
            pThread.shutDown();
        }
    }

    public synchronized void shutDown(){
        isShutDown = true;
        for(int threadIndex = 0; threadIndex< threadCounter;threadIndex++)
        {
            PThread pThread = (PThread) idleThread.get(threadIndex);
            pThread.shutDown();
        }
    }

    public synchronized void start(Runnable target)
    {
        PThread thread = null;
        if(idleThread.size()>0)
        {
            int lastIndex = idleThread.size() - 1;
            thread = (PThread) idleThread.get(lastIndex);
            idleThread.remove(lastIndex);
            thread.setTarget(target);

        }else
        {
            threadCounter++;
            thread = new PThread(target, "PThread #" + threadCounter, this);
            thread.start();
        }
    }
}
class PThread extends Thread{
    private  SimpleThreadPool threadPool;
    private  Runnable target;
    private boolean isShutDown = false;
    private boolean isIdle = false;
    public PThread(Runnable target, String name, SimpleThreadPool simpleThreadPool)
    {
        super(name);
        this.threadPool = simpleThreadPool;
        this.target = target;
    }

    public  Runnable getTarget()
    {
        return this.target;
    }

    public boolean isIdle()
    {
        return this.isIdle;
    }

    @Override
    public  void run()
    {
        while (!isShutDown)
        {
            isIdle = false;
            if(target != null)
            {
                target.run();
            }
        }

        isIdle = true;
        try {
            threadPool.repool(this);
            synchronized (this)
            {
                wait();
            }
        }catch (InterruptedException e)
        {

        }
        isIdle = false;
    }

    public synchronized void setTarget(Runnable newTarget)
    {
        target = newTarget;
        notifyAll();
    }

    public synchronized void shutDown()
    {
        isShutDown = true;
        notifyAll();
    }
}
class MyThread implements Runnable
{
    protected String name ;
    public MyThread()
    {}
    public MyThread(String name)
    {
        this.name = name;
    }
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        try {
            System.out.println(name);
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class test
{
    public static void main(String[] args) {
//        for(int i=0;i<1000;i++)
//        {
//            new Thread(new MyThread("testNoThreadPool"+i)).start();
//        }
        SimpleThreadPool simpleThreadPool = SimpleThreadPool.getInstance();
        for(int i=0;i<10;i++)
        {
             simpleThreadPool.start(new MyThread("testThreadPool"+i));
        }

       
    }
}

但是這個(gè)線程池的shutDown方法實(shí)際上無(wú)法讓線程停下,因?yàn)榫€程池中的idleThreads一直為空,所以實(shí)際上沒(méi)有設(shè)置線程的shutDown狀態(tài)歉摧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末灿意,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子扫沼,更是在濱河造成了極大的恐慌出爹,老刑警劉巖庄吼,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異严就,居然都是意外死亡霸褒,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)盈蛮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)废菱,“玉大人,你說(shuō)我怎么就攤上這事抖誉∈庵幔” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵袒炉,是天一觀的道長(zhǎng)旁理。 經(jīng)常有香客問(wèn)我,道長(zhǎng)我磁,這世上最難降的妖魔是什么孽文? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮夺艰,結(jié)果婚禮上芋哭,老公的妹妹穿的比我還像新娘。我一直安慰自己郁副,他們只是感情好减牺,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著存谎,像睡著了一般拔疚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上既荚,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天稚失,我揣著相機(jī)與錄音,去河邊找鬼恰聘。 笑死句各,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的憨琳。 我是一名探鬼主播诫钓,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼篙螟!你這毒婦竟也來(lái)了菌湃?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤遍略,失蹤者是張志新(化名)和其女友劉穎惧所,沒(méi)想到半個(gè)月后骤坐,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡下愈,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年纽绍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片势似。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拌夏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出履因,到底是詐尸還是另有隱情障簿,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布栅迄,位于F島的核電站站故,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏毅舆。R本人自食惡果不足惜西篓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望憋活。 院中可真熱鬧岂津,春花似錦、人聲如沸余掖。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)盐欺。三九已至,卻和暖如春仅醇,著一層夾襖步出監(jiān)牢的瞬間冗美,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工析二, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留粉洼,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓叶摄,卻偏偏與公主長(zhǎng)得像属韧,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蛤吓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容