From ec14aa5549d65c280f1e189b5d2aa775383df6fc Mon Sep 17 00:00:00 2001 From: caironglin <1226054531@qq.com> Date: Fri, 16 Dec 2022 15:41:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[test]=20=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E6=A0=B8=E5=BF=83=E5=8F=82=E6=95=B0=E4=B9=8B=E9=97=B4=E7=9A=84?= =?UTF-8?q?=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/com/neo/TreadPoolMain.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java diff --git a/spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java b/spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java new file mode 100644 index 0000000..552ca5a --- /dev/null +++ b/spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java @@ -0,0 +1,58 @@ +package com.neo; + + +import org.junit.Test; + +import java.time.Instant; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @program: spring-boot-examples + * @description: + * @author: RLWater + * @create: 2022-12-16 15:27 + **/ +public class TreadPoolMain { + + + private static final int CORE_POOL_SIZE = 5; + private static final int MAX_POOL_SIZE = 10; + private static final int QUEUE_CAPACITY = 100; // 当前线程数>CORE_POOL_SIZE+QUEUE_CAPACITY时,再判断(QUEUE_CAPACITY), + new ThreadPoolExecutor.CallerRunsPolicy()); + + for (int i = 0; i < 10; i++) { + executor.execute(() -> { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("CurrentThread name:" + Thread.currentThread().getName() + "date:" + Instant.now()); + }); + } + //终止线程池 + executor.shutdown(); + try { + executor.awaitTermination(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Finished all threads"); + } + +} -- Gitee From f518b550e2ddfe281f185451e71c2d5bc2e7d15a Mon Sep 17 00:00:00 2001 From: caironglin <1226054531@qq.com> Date: Fri, 16 Dec 2022 16:44:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[add]=20=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E5=92=8C=E4=BA=8C=E7=BA=A7=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/neo/config/NamingThreadFactory.java | 31 ++++++++++++++++++ .../src/test/java/com/neo/TreadPoolMain.java | 32 ++++++++++++++++--- 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 spring-boot-hello/src/main/java/com/neo/config/NamingThreadFactory.java diff --git a/spring-boot-hello/src/main/java/com/neo/config/NamingThreadFactory.java b/spring-boot-hello/src/main/java/com/neo/config/NamingThreadFactory.java new file mode 100644 index 0000000..c0e4fbb --- /dev/null +++ b/spring-boot-hello/src/main/java/com/neo/config/NamingThreadFactory.java @@ -0,0 +1,31 @@ +package com.neo.config; + + +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; +/** + * 线程工厂,它设置线程名称,有利于我们定位问题。 + */ +public final class NamingThreadFactory implements ThreadFactory { + + private final AtomicInteger threadNum = new AtomicInteger(); + private final ThreadFactory delegate; + private final String name; + + /** + * 创建一个带名字的线程池生产工厂 + */ + public NamingThreadFactory(ThreadFactory delegate, String name) { + this.delegate = delegate; + this.name = name; // TODO consider uniquifying this + } + + @Override + public Thread newThread(Runnable r) { + Thread t = delegate.newThread(r); + t.setName(name + " [#" + threadNum.incrementAndGet() + "]"); + return t; + } + +} diff --git a/spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java b/spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java index 552ca5a..fa5176c 100644 --- a/spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java +++ b/spring-boot-hello/src/test/java/com/neo/TreadPoolMain.java @@ -1,6 +1,7 @@ package com.neo; +import com.neo.config.NamingThreadFactory; import org.junit.Test; import java.time.Instant; @@ -25,6 +26,7 @@ public class TreadPoolMain { @Test public void main() { + //使用阿里巴巴推荐的创建线程池的方式 //通过ThreadPoolExecutor构造函数自定义参数创建 ThreadPoolExecutor executor = new ThreadPoolExecutor( @@ -35,13 +37,35 @@ public class TreadPoolMain { new ArrayBlockingQueue<>(QUEUE_CAPACITY), new ThreadPoolExecutor.CallerRunsPolicy()); + ThreadPoolExecutor executor2 = new ThreadPoolExecutor( + CORE_POOL_SIZE, + MAX_POOL_SIZE, + KEEP_ALIVE_TIME, + TimeUnit.SECONDS, + new ArrayBlockingQueue<>(QUEUE_CAPACITY), + new ThreadPoolExecutor.CallerRunsPolicy()); + for (int i = 0; i < 10; i++) { executor.execute(() -> { - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); + //try { + // Thread.sleep(2000); + // //executor2.execute(() -> { + // // try { + // // Thread.sleep(2000); + // // }catch (InterruptedException e){ + // // e.printStackTrace(); + // // } + // //}); + //} catch (InterruptedException e) { + // e.printStackTrace(); + //} + for (int j=0; j<10; j++){ + executor2.execute(() -> { + System.out.println("CurrentThread name:" + Thread.currentThread().getName()); + System.out.println("hello"); + }); } + System.out.println("CurrentThread name:" + Thread.currentThread().getName() + "date:" + Instant.now()); }); } -- Gitee