线程池的使用


java线程池
1.java通过Executors提供四种线程池,分别为
1.newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则创建新的线程
2.newFixedThreadPool 创建一个定长线程池,支持定时及周期性任务执行
3.newScheduledThreadPool 创建定长的线程池,支持定时及周期性任务的执行
4.newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务

代码演示

1.newCachedThreadPool

/**
	 * 创建一个可缓存线程池,如果线程池长度超过处理需要, 可灵活回收空闲线程,若无可回收,则创建新的线程,线程不定长
	 * 
	 * @throws Exception
	 */
	@Test
	public void testNewCachedThreadPool() throws Exception {
		ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
		for (int i = 0; i <= 10; i++) {
			final int index = i;
			Thread.sleep(3000L);
			/**
			 * 当取消线程的等待的时候,结果如下 Thread.sleep(3000L); pool-1-thread-1index0
			 * pool-1-thread-2index1 pool-1-thread-3index2 pool-1-thread-4index3
			 * pool-1-thread-5index4 pool-1-thread-2index5 pool-1-thread-4index7
			 * pool-1-thread-1index8 pool-1-thread-5index6 pool-1-thread-3index9
			 * pool-1-thread-6index10 可以看到有开启了6个线程
			 */
			newCachedThreadPool.execute(() -> System.out.println(Thread.currentThread().getName() + "index" + index));
			/**
			 * 打印出的结果如下
			 * pool-1-thread-1index0 
			 * pool-1-thread-1index1
			 * pool-1-thread-1index2 
			 * pool-1-thread-1index3 
			 * pool-1-thread-1index4
			 * pool-1-thread-1index5 
			 * pool-1-thread-1index6 
			 * pool-1-thread-1index7
			 * pool-1-thread-1index8 
			 * pool-1-thread-1index9
			 * pool-1-thread-1index10
			 * 表明用的是同一线程,线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。
			 */
		}
	}
2.newFixedThreadPool

/**
	 * 创建的是一个定长的线程池,可控制线程的最大并发数,当超出线程数的时候会在队列中等待
	 * 
	 * @throws Exception
	 * 
	 */
	@Test
	public void testNewFixedThreadPool() throws Exception {
		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);
		for (int i = 0; i <= 10; i++) {
			final int index = i;
			Thread.sleep(3000L);
			newFixedThreadPool.execute(() -> System.out.println(Thread.currentThread().getName() + "index" + index));
			/**
			 * pool-1-thread-1index0 
			 * pool-1-thread-2index1 
			 * pool-1-thread-3index2
			 * pool-1-thread-1index3 
			 * pool-1-thread-2index4 
			 * pool-1-thread-3index5
			 * pool-1-thread-1index6 
			 * pool-1-thread-2index7 
			 * pool-1-thread-3index8
			 * pool-1-thread-1index9 
			 * pool-1-thread-2index10
			 * 得出的结果中我们可以看到一直都是这三个线程在等待
			 */
			//定长线程池的大小最好根据系统资源进行设置。如
			int availableProcessors = Runtime.getRuntime().availableProcessors();
			//可参考PreloadDataCache。
		}
	}

3.newScheduledThreadPool

/**
	 * 创建一个定长的线程池,支持定时及周期性执行任务
	 * @throws Exception 
	 */
	@Test
	public void testNewScheduledThreadPool() throws Exception{
		ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
		Long start = new Date().getTime();
		scheduledThreadPool.schedule(new Runnable() {
			@Override
			public void run() {
				System.out.println("延迟3s后执行");
			}
		}, 3, TimeUnit.SECONDS);
		Long end = new Date().getTime();
		Thread.currentThread().sleep(4000L);
		System.out.println(end-start);
	}
4.newSingleThreadExecutor

/**
	 * 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务
	 */
	@Test
	public void testNewSingleThreadPool() {
		ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
		for (int i = 0; i < 10; i++) {
			final int index = i;
			newSingleThreadExecutor
					.execute(() -> System.out.println(Thread.currentThread().getName() + ", 输出的值" + index));
		}
		/**
		 * 输出的结果展示 
		 * pool-1-thread-1, 输出的值0 
		 * pool-1-thread-1, 输出的值1
		 * pool-1-thread-1, 输出的值2 
		 * pool-1-thread-1, 输出的值3 
		 * pool-1-thread-1, 输出的值4
		 * pool-1-thread-1, 输出的值5 
		 * pool-1-thread-1, 输出的值6 
		 * pool-1-thread-1, 输出的值7
		 * pool-1-thread-1, 输出的值8 
		 * pool-1-thread-1, 输出的值9
		 */

	}
线程的好处:

1.减少创建和销毁的次数

2.更加充分使用CPU,在多核CPU的时代,单线程是非常浪费的

3.可以根据系统的承受能力,调整线程池中的工作线程数目,防止因为消耗过多的内存,导致服务器挂掉(每个线程需要大约1MB内存,线程开的越多,消耗的内存也就越大,最后死机)


比较重要的几个类:

ExecutorService: 真正的线程池接口。

ScheduledExecutorService: 能和Timer/TimerTask类似,解决那些需要任务重复执行的问题。

ThreadPoolExecutor: ExecutorService的默认实现。

ScheduledThreadPoolExecutor: 继承ThreadPoolExecutor的ScheduledExecutorService接口实现,周期性任务调度的类实现。





具体可参照https://www.cnblogs.com/ruiati/p/6134131.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值