1. 程式人生 > >java多執行緒之執行緒池執行器

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());
		}
	}
	
}