java多线程之线程池执行器

package com.np.ota.test.executor;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * java5之后,用ThreadPoolExecutor去管理线程池
 * 5种创建线程池的方法
 * 多线程,或者说时线程池,就是用来同时执行多任务的,
 * 其本质就是一个以空间换时间的方案。
 * 用创建线程消耗的更多内存去换取同时处理多个任务时间上的减少。
 * @author luke
 */
public class ExecutorsTest {

	public static void main(String[] args) {
		//ExecutorsTest.createThreadPoolMethod1();
		//ExecutorsTest.createThreadPoolMethod2();
		//ExecutorsTest.createThreadPoolMethod3();
		ExecutorsTest.createThreadPoolMethod4();
	}
	
	public static void createThreadPoolMethod1(){
		ExecutorService threadPool = Executors.newFixedThreadPool(4);//固定线程池大小
		MyTask t1 = new MyTask();
		MyTask t2 = new MyTask();
		MyTask t3 = new MyTask();
		MyTask t4 = new MyTask();
		MyTask t5 = new MyTask();
		threadPool.execute(t1);
		threadPool.execute(t2);
		threadPool.execute(t3);
		threadPool.execute(t4);
		threadPool.execute(t5);
		threadPool.shutdown();
		//结果:
		/* pool-1-thread-2正在执行。。。
			pool-1-thread-4正在执行。。。
			pool-1-thread-2正在执行。。。
			pool-1-thread-3正在执行。。。
			pool-1-thread-1正在执行。。。*/
		
		//线程2在线程池里面被复用
		
	}
	
	public static void createThreadPoolMethod2(){
		ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();//可变线程池大小
		MyTask t1 = new MyTask();
		MyTask t2 = new MyTask();
		MyTask t3 = new MyTask();
		MyTask t4 = new MyTask();
		MyTask t5 = new MyTask();
		threadPool.execute(t1);
		threadPool.execute(t2);
		threadPool.execute(t3);
		threadPool.execute(t4);
		threadPool.execute(t5);
		threadPool.shutdown();
		//结果:
		/* pool-1-thread-2正在执行。。。
			pool-1-thread-1正在执行。。。
			pool-1-thread-4正在执行。。。
			pool-1-thread-5正在执行。。。
			pool-1-thread-3正在执行。。。*/
		
		//创建可变大小的线程池
		
	}
	
	public static void createThreadPoolMethod3(){
		ExecutorService threadPool = Executors.newSingleThreadExecutor();//单线程线程池
		MyTask t1 = new MyTask();
		MyTask t2 = new MyTask();
		MyTask t3 = new MyTask();
		MyTask t4 = new MyTask();
		MyTask t5 = new MyTask();
		threadPool.execute(t1);
		threadPool.execute(t2);
		threadPool.execute(t3);
		threadPool.execute(t4);
		threadPool.execute(t5);
		threadPool.shutdown();
		//结果:
		/* pool-1-thread-1正在执行。。。
			pool-1-thread-1正在执行。。。
			pool-1-thread-1正在执行。。。
			pool-1-thread-1正在执行。。。
			pool-1-thread-1正在执行。。。*/
		//单线程池,5个任务被排队执行
		System.out.println("----------");
	}
	
	public static void createThreadPoolMethod4(){
		ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(4);//任务调度线程池
		MyTask t3 = new MyTask();
		MyTask t4 = new MyTask();
		MyTask t5 = new MyTask();
		//threadPool.schedule(t3, 10, TimeUnit.SECONDS);
		//threadPool.schedule(t4, 10, TimeUnit.SECONDS);
		
		//第三个参数,固定周期内必须执行一次,例如设置period为一小时,执行的线程任务需要10分钟
		//那么执行开始时间为 12:00,13:00,14:00
		//threadPool.scheduleAtFixedRate(t5, 10,7, TimeUnit.SECONDS);
		
		//第三个参数,每次执行时延迟时间,例如设置delay为一小时,执行的线程任务需要10分钟
		//那么执行开始时间为 12:00,13:10,14:20
		threadPool.scheduleWithFixedDelay(t5, 10,5, TimeUnit.SECONDS);
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//任务调度需要线程池一直存在,调用shutdown(),后面等待的任务将不再执行
		//threadPool.shutdown();
		
		System.out.println("------");
		
		//结果:
		/* pool-1-thread-1正在执行。。。Wed Mar 14 11:46:40 CST 2018
			pool-1-thread-1正在执行。。。Wed Mar 14 11:46:45 CST 2018
			pool-1-thread-2正在执行。。。Wed Mar 14 11:46:50 CST 2018
			pool-1-thread-2正在执行。。。Wed Mar 14 11:46:55 CST 2018。*/
		
	}
	
	static class  MyTask implements Runnable {
		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + "正在执行。。。"+new Date());
		}
	}
	
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值