From 4bb12db1b856ca562cfe5be9a94b82321780a6aa Mon Sep 17 00:00:00 2001 From: wangweicugw <38103831+wangweicugw@users.noreply.github.com> Date: Mon, 26 Feb 2024 08:47:06 +0800 Subject: [PATCH] Creating a thread pool is not recommended using Executors, but instead through ThreadPoolExecutor (#153) --- src/test/java/com/jd/jdbc/cache/LRUCacheTest.java | 8 ++++---- .../jd/jdbc/sqlparser/NormalizeRewritePutTest.java | 3 +-- .../com/jd/jdbc/table/ConcurrentSplitTableTest.java | 12 +----------- .../jd/jdbc/vitess/VitessDriverConsolidatorTest.java | 3 +-- .../jd/jdbc/vitess/VitessDriverExecuteBatchTest.java | 3 +-- .../VitessDriverAllowMultiQuerieTest.java | 6 ++++-- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/test/java/com/jd/jdbc/cache/LRUCacheTest.java b/src/test/java/com/jd/jdbc/cache/LRUCacheTest.java index 103de45..1302f70 100644 --- a/src/test/java/com/jd/jdbc/cache/LRUCacheTest.java +++ b/src/test/java/com/jd/jdbc/cache/LRUCacheTest.java @@ -18,14 +18,14 @@ import com.jd.jdbc.util.cache.lrucache.LRUCache; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; import org.junit.Assert; -import org.junit.Test; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import org.junit.Test; public class LRUCacheTest { @@ -136,7 +136,7 @@ public void testConcurrency() throws InterruptedException { CacheValue v1 = new CacheValue("v1"); cache.set("k1", v1); - ExecutorService executorService = Executors.newSingleThreadExecutor(); + ExecutorService executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); executorService.submit(() -> { if (cache.get("k1") != null) { try { diff --git a/src/test/java/com/jd/jdbc/sqlparser/NormalizeRewritePutTest.java b/src/test/java/com/jd/jdbc/sqlparser/NormalizeRewritePutTest.java index cd8a32a..fa6981b 100644 --- a/src/test/java/com/jd/jdbc/sqlparser/NormalizeRewritePutTest.java +++ b/src/test/java/com/jd/jdbc/sqlparser/NormalizeRewritePutTest.java @@ -30,7 +30,6 @@ import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import lombok.AllArgsConstructor; import org.junit.Assert; @@ -80,7 +79,7 @@ public void testNormalizeRewritePut() throws SQLException { public void testConcurrency() throws InterruptedException { int tSize = 100; CountDownLatch cdl = new CountDownLatch(tSize); - ExecutorService executorService = Executors.newFixedThreadPool(10); + ExecutorService executorService = getThreadPool(10, 10); executorService.submit(() -> { try { String name = Thread.currentThread().getName(); diff --git a/src/test/java/com/jd/jdbc/table/ConcurrentSplitTableTest.java b/src/test/java/com/jd/jdbc/table/ConcurrentSplitTableTest.java index b45f997..d9568a2 100644 --- a/src/test/java/com/jd/jdbc/table/ConcurrentSplitTableTest.java +++ b/src/test/java/com/jd/jdbc/table/ConcurrentSplitTableTest.java @@ -29,10 +29,7 @@ import java.sql.Statement; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.lang3.RandomUtils; import org.junit.After; import org.junit.AfterClass; @@ -68,14 +65,7 @@ private static String getRandomKey() { @BeforeClass public static void initClass() { - pool = Executors.newFixedThreadPool(10, new ThreadFactory() { - private final AtomicInteger threadNumber = new AtomicInteger(1); - - @Override - public Thread newThread(final Runnable r) { - return new Thread(r, threadNumber.getAndIncrement() + ""); - } - }); + pool = getThreadPool(10, 10); } @AfterClass diff --git a/src/test/java/com/jd/jdbc/vitess/VitessDriverConsolidatorTest.java b/src/test/java/com/jd/jdbc/vitess/VitessDriverConsolidatorTest.java index d15d987..26df7d9 100755 --- a/src/test/java/com/jd/jdbc/vitess/VitessDriverConsolidatorTest.java +++ b/src/test/java/com/jd/jdbc/vitess/VitessDriverConsolidatorTest.java @@ -33,7 +33,6 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; @@ -70,7 +69,7 @@ public class VitessDriverConsolidatorTest extends TestSuite { @BeforeClass public static void initClass() { - executorService = Executors.newFixedThreadPool(10); + executorService = getThreadPool(10, 10); } @AfterClass diff --git a/src/test/java/com/jd/jdbc/vitess/VitessDriverExecuteBatchTest.java b/src/test/java/com/jd/jdbc/vitess/VitessDriverExecuteBatchTest.java index 37abc90..43ba321 100644 --- a/src/test/java/com/jd/jdbc/vitess/VitessDriverExecuteBatchTest.java +++ b/src/test/java/com/jd/jdbc/vitess/VitessDriverExecuteBatchTest.java @@ -24,7 +24,6 @@ import java.sql.Statement; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; import org.junit.Assert; @@ -59,7 +58,7 @@ public void clean() throws SQLException { public void testConcurrency() throws InterruptedException, SQLException { int threadCount = 10; CountDownLatch latch = new CountDownLatch(threadCount); - ExecutorService service = Executors.newFixedThreadPool(threadCount); + ExecutorService service = getThreadPool(10, 10); AtomicInteger inserted = new AtomicInteger(0); for (int i = 0; i < 10; i++) { final int finalI = i; diff --git a/src/test/java/com/jd/jdbc/vitess/allowMultiQueries/VitessDriverAllowMultiQuerieTest.java b/src/test/java/com/jd/jdbc/vitess/allowMultiQueries/VitessDriverAllowMultiQuerieTest.java index 674dad0..9f67496 100644 --- a/src/test/java/com/jd/jdbc/vitess/allowMultiQueries/VitessDriverAllowMultiQuerieTest.java +++ b/src/test/java/com/jd/jdbc/vitess/allowMultiQueries/VitessDriverAllowMultiQuerieTest.java @@ -26,11 +26,13 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -48,7 +50,7 @@ public class VitessDriverAllowMultiQuerieTest extends TestSuite { @BeforeClass public static void init() { - executor = Executors.newFixedThreadPool(10); + executor = getThreadPool(10, 10); } @AfterClass