使用common-pool实现的一个简单的线程池

使用apache的commons-pool完成了一个简单的可伸缩的线程池,实现的功能为: 
1、可以为线程池设定最大线程数,当有请求的时候,从池从取出一个线程来进行处理,当处理完成后,再将线程重新放入到池中。如果池中的线程不够地话,系统会自动地增加池中线程的数量,但总量不超过最大线程数。 

2、定时查看线程池,当线程池中的线程过长时间未被使用,则自动销毁它。


/**
 * 创建销毁线程池中对象的工厂                                                  
 * @author Amylh           
 */
import org.apache.commons.pool.PoolableObjectFactory;

public class MyPoolableObjectFactory implements PoolableObjectFactory {
	private boolean isDebug = true;

	public Object makeObject() throws Exception {
		SimpleThread simpleThread = new SimpleThread();
		simpleThread.start();
		debug("创建线程:" + simpleThread.getName());
		return simpleThread;
	}

	public void destroyObject(Object obj) throws Exception {
		if (obj instanceof SimpleThread) {
			SimpleThread simpleThread = (SimpleThread) obj;
			debug("销毁线程:" + simpleThread.getName());
			simpleThread.destroy();
		}
	}

	public boolean validateObject(Object obj) {
		return true;
	}

	public void activateObject(Object obj) throws Exception {
	}

	public void passivateObject(Object obj) throws Exception {
	}

	public void setIsDebug(boolean isDebug) {
		this.isDebug = isDebug;
	}

	public boolean getIsDebug() {
		return isDebug;
	}

	private void debug(String debugInfo) {
		if (isDebug) {
			System.out.println(debugInfo);
		}
	}
}

/**
 * 一个简单的线程                                                  
 * @author Amylh           
 */
import org.apache.commons.pool.impl.*;

class SimpleThread extends Thread {
	private boolean isRunning;
	private GenericObjectPool pool;

	public boolean getIsRunning() {
		return isRunning;
	}

	public synchronized void setIsRunning(boolean flag) {
		isRunning = flag;
		if (flag) {
			this.notify();
		}
	}

	public void setPool(GenericObjectPool pool) {
		this.pool = pool;
	}

	public SimpleThread() {
		isRunning = false;
	}

	public void destroy() {
		System.out.println("destroy中");
		this.interrupt();
	}

	public synchronized void run() {
		try {
			while (true) {
				if (!isRunning) {
					this.wait();
				} else {
					System.out.println(this.getName() + "开始处理");
					sleep(5000);
					System.out.println(this.getName() + "结束处理");
					setIsRunning(false);
					try {
						pool.returnObject(this);
					} catch (Exception ex) {
						System.out.println(ex);
					}
				}
			}
		} catch (InterruptedException e) {
			System.out.println("我被Interrupted了,所以此线程将被关闭");
			return;
		}
	}
}

/**
 * 此线程池的测试类                                                
 * @author Amylh           
 */
import org.apache.commons.pool.impl.GenericObjectPool;

public class TestThreadPool {
	public static void main(String[] args) {
		MyPoolableObjectFactory factory = new MyPoolableObjectFactory();
		GenericObjectPool pool = new GenericObjectPool(factory);
		pool.setMaxActive(20); // 能从池中借出的对象的最大数目
		pool.setMaxIdle(20); // 池中可以空闲对象的最大数目
		pool.setMaxWait(100); // 对象池空时调用borrowObject方法,最多等待多少毫秒
		pool.setTimeBetweenEvictionRunsMillis(600000);// 间隔每过多少毫秒进行一次后台对象清理的行动
		pool.setNumTestsPerEvictionRun(-1);// -1表示清理时检查所有线程
		pool.setMinEvictableIdleTimeMillis(3000);// 设定在进行后台对象清理时,休眠时间超过了3000毫秒的对象为过期
		for (int i = 0; i < 20; i++) {
			try {
				SimpleThread simpleThread = (SimpleThread) pool.borrowObject();
				simpleThread.setPool(pool);
				simpleThread.setIsRunning(true);
			} catch (Exception ex) {
				System.out.println(ex);
			}
		}
		try {
			Thread.sleep(8000); // 休息一会儿,再使用线程池
		} catch (InterruptedException ex1) {
		}
		System.out.println("------------------------------");
		for (int i = 0; i < 10; i++) {
			try {
				SimpleThread simpleThread = (SimpleThread) pool.borrowObject();
				simpleThread.setPool(pool);
				simpleThread.setIsRunning(true);
			} catch (Exception ex) {
				System.out.println(ex);
			}
		}
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值