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());
}
}
}
java多线程之线程池执行器
最新推荐文章于 2023-07-14 20:54:44 发布