您的位置:首页 > 编程语言 > Java开发

JDK线程池组件使用示例

2011-03-30 15:22 309 查看
public static void main(String[] args) throws Exception
{
// 构造一个自定义参数的线程池
//ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 40, 30, TimeUnit.SECONDS, new ArrayBlockingQueue(3), new ThreadPoolExecutor.DiscardOldestPolicy());
//构造一个默认方式的线程池
ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();

for(int i=1; i<=10; i++)
{
// 产生一个任务,并将其加入到线程池
String task = "task@ " + i;
threadPool.execute(new ThreadPoolTask(task));
}
}
//线程所执行的任务
public static class ThreadPoolTask implements Runnable
{
private Object threadPoolTaskData;
ThreadPoolTask(Object tasks){
this.threadPoolTaskData = tasks;
}
public void run()
{
System.out.println("start .." + threadPoolTaskData);
threadPoolTaskData = null;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: