欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线程池示例

程序员文章站 2022-05-01 14:18:54
...

 

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));
	}

}

 

 

相关标签: 线程池