Java并发编程学习笔记

《JAVA 性能优化》上面的源码-简单线程池的实现-待优化

2019-07-16  本文已影响5人  熊熊要更努力
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));
        }

       
    }
}

但是这个线程池的shutDown方法实际上无法让线程停下,因为线程池中的idleThreads一直为空,所以实际上没有设置线程的shutDown状态。

上一篇下一篇

猜你喜欢

热点阅读