一个简单的JAVA线程池实例

package com.spreadcomm.game.net;

import java.util.LinkedList;
import java.util.List;

class ThreadManager {
	/**Working thread object in this pool*/
	private ExecuteThread[] mWorkThreadQueue;
	
	/**Save the user thread, which implement by user*/
	private List<Runnable> mWaitingQueue = new LinkedList<Runnable> ();
	
	/**Indicate the pool life cycle*/
	private boolean mIsActive = true;
	
	/**Control waiting queue thread safely*/
	private Object mTicket = new Object();
	
	/**
	 * Default only one working thread in this pool
	 * @param name
	 */
	public ThreadManager(String name){
		this(name,1);
	}
	
	/**
	 * Create working thread queue, according to special account
	 * 
	 * @param name
	 * @param threadcount
	 */
	public ThreadManager(String name, int threadcount) {
		mWorkThreadQueue = new ExecuteThread[threadcount];
		for (int i = 0; i < mWorkThreadQueue.length; i++) {
			mWorkThreadQueue[i] = new ExecuteThread(name, this, i);
			mWorkThreadQueue[i].setDaemon(true);
			mWorkThreadQueue[i].start();
		}
		
		// when virtual machine is idle state, it will been invoke
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				if (mIsActive) {
					shutdown();
				}
			}
		});
	}
	
	/**
	 * push thread to waiting queue 
	 * @param task
	 */
	public synchronized void invokeLater(Runnable task) {
		synchronized (mWaitingQueue) {
			mWaitingQueue.add(task);
		}
		synchronized (mTicket) {
			mTicket.notify();
		}
	}
	
	/**
	 * poll thread in waiting queue
	 * 
	 * @return
	 */
	public Runnable poll(){
		while(mIsActive){
			synchronized(mWaitingQueue){
				if (mWaitingQueue.size() > 0) {
					Runnable task = mWaitingQueue.remove(0);
					if (null != task) {
						return task;
					}
				}
			}
			
			synchronized (mTicket) {
				try {
					mTicket.wait();
				} catch (InterruptedException ex) {
					ex.printStackTrace();
				}
			}
		}
		return null;
	}
	
	/**
	 * Release all of working thread
	 */
	public synchronized void shutdown() {
		if (mIsActive) {
			mIsActive = false;
			
			for (ExecuteThread thread : mWorkThreadQueue) {
				thread.shutdown();
			}
			
			synchronized (mTicket) {
				mTicket.notify();
			}
		} else {
			throw new IllegalStateException("Already shutdown");
		}
	}
}

/**
 * This is a work thread, replay to read waiting 
 * queue and run it until it is done
 * 
 * @author
 *
 */
class ExecuteThread extends Thread {
	ThreadManager mThreadPoolManager;
	private boolean mAlive = true;
	
	ExecuteThread(String name, ThreadManager q, int index) {
		super(name + "[" + index + "]");
		mThreadPoolManager = q;
	}
	
	public void shutdown() {
		mAlive = false;
	}
	
	/**
	 * Get waiting thread from waiting queue
	 */
	public void run() {
		while (mAlive) {
			Runnable task = mThreadPoolManager.poll();
			if (null != task) {
				try {
					task.run();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值