线程池示例

 

1. 创建线程池 ThreadPool.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

/**
 * @ClassName: ThreadPool
 * @Description: 线程池
 * @author 
 * @company 
 * @date 2013-3-13
 * @version V1.0
 */

public final class ThreadPool {

	private static final Logger log = Logger.getLogger(ThreadPool.class);

	private ExecutorService pool = null;

	private static final ThreadPool instance = new ThreadPool();

	/** 线程池维护线程的最少数量 */
	private static final int corePoolSize = 10;
	/** 线程池维护线程的最大数量 */
	private static final int maximumPoolSize = 15;
	/** 线程池维护线程所允许的空闲时间.当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间 */
	private static final long keepAliveTime = 60;

	private ThreadPool() {
	}

	/**
	 * @Title: getInstance
	 * @Description: 初始化线程池
	 * @return:ThreadPool
	 * @author 
	 * @date 2013-3-13
	 */
	public static ThreadPool getInstance() {
		try {
			if (null == instance.pool) {

				synchronized (instance) {

					if (null == instance.pool) {
						instance.pool = new ThreadPoolExecutor(corePoolSize,
								maximumPoolSize, keepAliveTime,
								TimeUnit.SECONDS,
								new LinkedBlockingQueue<Runnable>());
					}
				}
			}
		} catch (Exception e) {
			log.error("线程池初始化失败:" + e.getMessage(), e);
		}

		return instance;
	}

	public ExecutorService getPool() {
		return pool;
	}

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

 

2. 测试用例 TestVectorThread.java

import java.util.Vector;

public class TestVectorThread implements Runnable {

	public TestVectorThread() {}
	
	private Vector<String> vc;
	
	public TestVectorThread(Vector<String> vc) {
		this.vc = vc;
	}
	
	@Override
	public void run() {
		try {
			test(vc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void test(Vector<String> vc) throws Exception {

		CommonMethod.getLast(vc);
		CommonMethod.deleteLast(vc);
	}
}

 

3. 调用的方法  CommonMethod.java

import java.util.Vector;

public final class CommonMethod {

	private CommonMethod() {
	}

	/**
	 * @Title: getLast
	 * @Description: 获取最后一个元素
	 * @param vc 元素集合
	 * @return:String
	 * @author 
	 * @date 2013-3-13
	 */
	public static String getLast(Vector<String> vc) {

		synchronized (vc) {
			int lastIndex = vc.size() - 1;
			return vc.get(lastIndex);
		}
	}

	/**
	 * @Title: deleteLast
	 * @Description: 删除最后一个元素
	 * @param vc 元素集合
	 * @author 
	 * @date 2013-3-13
	 */
	public static void deleteLast(Vector<String> vc) {

		synchronized (vc) {
			int lastIndex = vc.size() - 1;
			vc.remove(lastIndex);
		}
	}
}

 

4. 测试 Test1.java

public class Test1 {

	@Test
	public void test1() {
		
		Vector<String> vc = new Vector<String>(10);

		vc.add("aaaa");
		vc.add("bbb");
		vc.add("ccc");
		vc.add("ddd");
		vc.add("eee");
		vc.add("fff");
		vc.add("ggg");
		vc.add("hhh");
		vc.add("iii");
		vc.add("jjj");
		vc.add("kkk");

		ThreadPool.getInstance().getPool().execute(new TestVectorThread(vc));
	}

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值