Java实现的 线程池

由于最近开始学习java,用到了线程池,按照之前c++的写法写出此java版的线程池

TaskRunnale实现相关任务的接口,具体要实现什么任务在相应的run函数中实现。

package threadpool;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

class TaskRunnale implements Runnable 
{
    public void run() 
    {
        System.out.println(this.hashCode() + "Testrunner in Works");
        //此方法使本线程睡眠5秒
        synchronized (this) 
        {
            try 
            {
                wait(1000);//等待5秒时间
            }
            catch (InterruptedException ioe) 
            {
            }
        }
        System.out.println(this.hashCode() + "Testrunner out Works");
    }
}

class Thread_InPool extends Thread
{
	private ThreadPool  pool_     = null;  // 池中线程需要知道自己所在的池
    private Runnable 	target_   = null;   // 线程的任务
    private boolean 	shutDown_ = false;
    private boolean 	idle_     = false;//设置是否让线程处于等待状态
    
    public Thread_InPool(ThreadPool spool)
    {
    	
    	super();
    	pool_ = spool; 
    	start();
    }
    
    public void run() 
    {
        // 这个循环不能结束,除非池类要求线程结束
        // 每一次循环都会执行一次池类分配给的任务target
        while (!shutDown_) 
        {  

            idle_ = false;
            if (target_ != null) 
            {
                target_.run();  // 运行target中的代码
                target_ = null;           
	            
	            if(pool_.SwitchThread(this) == 1)
	                continue;

            }
            else
            {
            	idle_ = true;
            	 synchronized (this) 
                 {
                     try 
                     {
                         wait(2000);//等待2秒时间
                     }
                     catch (InterruptedException ioe) 
                     {
                     }
                 }
            }
            
        }
        //循环这里不能结束,否则线程结束,资源被VM收回,
        //就无法起到线程池的作用了
    }
    
    
    public synchronized void setTarget(java.lang.Runnable newTarget) 
    {//设置新的target,并唤醒睡眠中的线程
        target_ = newTarget;  // 新任务
        notifyAll();          // 唤醒睡眠的线程
    }
    
    public synchronized void shutDown()
    {
        shutDown_ = true;
        notifyAll();
    }
}

public class ThreadPool {
	
    private static ThreadPool instance_ 	= null;
    public static final int LOW_PRIORITY 	= 0; 
    public static final int NORMAL_PRIORITY = 1;
    public static final int HIGH_PRIORITY	= 2;
    
    private static int          	m_Threadcount = 4;
    private List<Thread_InPool>  	m_IdleThread = new ArrayList<Thread_InPool>();  //空闲的线程
    private List<Thread_InPool> 	m_ActiveThread = new ArrayList<Thread_InPool>();//处于任务执行的线程
    private Queue<Runnable>      	m_RunableTarger = new ArrayDeque<Runnable>(); ;//任务队列,此处用Queue,是考虑到可以有任务优先级的扩展

    private ThreadPool(int initcount)
    {
    	m_Threadcount = initcount > 0?initcount:4;
    	for(int i = 0 ;i< m_Threadcount ; i++)
    	{
    		m_IdleThread.add(new Thread_InPool(this));
    	}
    }
	  
    public static ThreadPool Get_Instance() {
        if (instance_ == null)
            instance_ = new ThreadPool(3);
        return instance_;
    }
    
    public synchronized void DestroyPool()  
    {
    	for(int i = 0 ;i< m_IdleThread.size();i++)
    	{
    		
    		Thread_InPool dthread = m_IdleThread.get(i);
    		dthread.shutDown();
    	}
    	for(int i = 0 ;i< m_ActiveThread.size();i++)
    	{
    		
    		Thread_InPool dthread = m_ActiveThread.get(i);
    		dthread.shutDown();
    	}
    	
    }
    
    public synchronized void InsertTarget(java.lang.Runnable newTarget)  //插入新的任务
    {
    	if(newTarget == null) return;
    	
    	if(!m_IdleThread.isEmpty())
    	{
    		Thread_InPool i_thread = null;
    		i_thread = m_IdleThread.get(m_IdleThread.size()-1);
    		m_IdleThread.remove(i_thread);
    		i_thread.setTarget(newTarget);
    		m_ActiveThread.add(i_thread);
    	}
    	else
    	{
    		m_RunableTarger.add(newTarget);
    	}
    }
    
    public synchronized int SwitchThread(Thread_InPool switchThread)  //任务结束后,如果任务队列不为空,则继续执行新的任务,否则入空队列
    {
    	int rvalue = 0;
    	if(!m_RunableTarger.isEmpty())
    	{
    		java.lang.Runnable o_Target = m_RunableTarger.poll();
    		if(o_Target != null)
    		{
    			switchThread.setTarget(o_Target);
    			rvalue = 1;
    		}
    	}
    	else
    	{
    		m_IdleThread.add(switchThread);
    		m_ActiveThread.remove(switchThread);
    	}
		return rvalue;
    }
}

 如果代码存在什么问题,欢迎指正,相关学习 

posted @ 2016-08-08 17:16  fireflyover  阅读(391)  评论(0编辑  收藏  举报