package com.stock.test.base.thread; import org.junit.Test; import java.util.concurrent.*; /** * * 线程池实例 * */ public class ThreadPoolTest { /** * 固定大小的线程,创建是给定线城池大小 * * 如果 */ @Test public void newFixedThreadPool(){ // 固定大小的线程,创建是给定线城池大小,线程池线程都在执行,则新进入的线程进入任务队列中等待 ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.execute(new TestThread("任务--1")); executorService.execute(new TestThread("任务--2")); executorService.execute(new TestThread("任务--3")); executorService.execute(new TestThread("任务--4")); } /**创建一个线程的线程池,如果空闲则执行,否则进入任务队列等待被自行*/ @Test public void singleThreadEexecutor() throws InterruptedException { // 创建一个线程的线程池,如果空闲则执行,否则进入任务队列等待被自行 ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(new TestThread("任务--1")); executorService.execute(new TestThread("任务--2")); executorService.execute(new TestThread("任务--3")); executorService.execute(new TestThread("任务--4")); Thread.sleep(100000); } /**缓存的线程池*/ @Test public void cacheThreadPool() throws InterruptedException { // 创建一个可根据实际情况调整线程个数的线程池,不限制最大线程数,若无任务则不创建线程; // 若有任务,有空闲线程则执行,无空闲线程,则创建新线程执行 // 空闲线程60s后没有任务可执行则回收 ExecutorService cacheThreadPool = Executors.newCachedThreadPool(); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(new TestThread("任务--1")); executorService.execute(new TestThread("任务--2")); executorService.execute(new TestThread("任务--3")); executorService.execute(new TestThread("任务--4")); Thread.sleep(100000); } /***/ @Test public void workStealingPool() throws InterruptedException { // parallelism/'pærəlɛl'ɪzəm/ n(平行,类似,对比) // stealing /'stilɪŋ/n. 偷窃;贼赃;偷垒; adj. 有偷窃行为的 ; v. 偷盗;抄袭(steal的ing形式) ExecutorService workStealingPool = Executors.newWorkStealingPool(10); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(new TestThread("任务--1")); executorService.execute(new TestThread("任务--2")); executorService.execute(new TestThread("任务--3")); executorService.execute(new TestThread("任务--4")); Thread.sleep(100000); } @Test public void scheduleExecutorService() throws InterruptedException { // schedule/ˈskedʒʊl; skɛdʒʊl/ n(计划,时间表,一览表) v(安排,预定) ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10); // 初始化延迟5秒,间隔1秒执行一次,TimeUnit时间单位 // scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { // @Override // public void run() { // System.out.println("run"); // } // },5,1, TimeUnit.SECONDS); scheduledExecutorService.schedule(new Runnable() { @Override public void run() { System.out.println("run1"); } },2, TimeUnit.SECONDS); // scheduledExecutorService.scheduleAtFixedRate(new Runnable() { // @Override // public void run() { // System.out.println("run3"); // } // },1,2,TimeUnit.SECONDS); Thread.sleep(100000); } /** * 自定义线程池 */ @Test public void definedExecutorService() throws InterruptedException { BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(20); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,3,60l,TimeUnit.MILLISECONDS,blockingQueue); threadPoolExecutor.execute(new TestThread("任务--1")); threadPoolExecutor.execute(new TestThread("任务--2")); threadPoolExecutor.execute(new TestThread("任务--3")); threadPoolExecutor.execute(new TestThread("任务--4")); while (threadPoolExecutor.getQueue().size() > 0){ System.out.println(blockingQueue.size()); } Thread.sleep(1000000); } public class TestThread implements Runnable{ private String name; public TestThread(String name){ this.name=name; } @Override public void run() { System.out.println(name); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
4、多线程(线程池)
最新推荐文章于 2024-11-18 12:10:44 发布