现在的位置: 首页 > 综合 > 正文

ThreadPool线程池

2018年02月13日 ⁄ 综合 ⁄ 共 830字 ⁄ 字号 评论关闭
package com.entel.research;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest
{
	public static void main(String[] args)
	{
//		ExecutorService threadPool = Executors.newFixedThreadPool(3);
//		ExecutorService threadPool = Executors.newCachedThreadPool();
		ExecutorService threadPool = Executors.newSingleThreadExecutor();
		for(int i=1;i<=10;i++)
		{
			final int task = i;
			threadPool.execute(new Runnable()
			{
				@Override
				public void run()
				{
					for(int j=1;j<10;j++)
					{
						System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task" + task);
						System.out.println("The End!!!");
					}
				}
			});
		}
		System.out.println("all of task has committed");
//		threadPool.shutdown();
		
		Executors.newScheduledThreadPool(3).schedule(new Runnable()
		{
			@Override
			public void run()
			{
				System.out.println("bombing");
			}
		}, 1, TimeUnit.SECONDS);
	}
}

抱歉!评论已关闭.