`
lin5061
  • 浏览: 80744 次
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
ThreadPoolExcutor JDK1.5中线程池研究及例子
package com.ibm.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThreadPool {
	private static int produceTaskSleepTime = 2000;

	public static void main(String[] args) {
		// 构造一个线程池
		ThreadPoolExecutor producerPool = new ThreadPoolExecutor(2, 4, 0,
				TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
				new ThreadPoolExecutor.DiscardOldestPolicy());

		// 每隔produceTaskSleepTime的时间向线程池派送一个任务。
		int i = 1;
		while (true) {
			try {
				Thread.sleep(produceTaskSleepTime);

				String task = "task@ " + i;
				System.out.println("put " + task);

				producerPool.execute(new ThreadPoolTask(task));

				i++;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

Global site tag (gtag.js) - Google Analytics