Skip to content

Commit

Permalink
Creating a thread pool is not recommended using Executors, but instea…
Browse files Browse the repository at this point in the history
…d through ThreadPoolExecutor (#153)
  • Loading branch information
wangweicugw authored Feb 26, 2024
1 parent 5efbbe9 commit 4bb12db
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/test/java/com/jd/jdbc/cache/LRUCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 1 addition & 11 deletions src/test/java/com/jd/jdbc/table/ConcurrentSplitTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,7 +69,7 @@ public class VitessDriverConsolidatorTest extends TestSuite {

@BeforeClass
public static void initClass() {
executorService = Executors.newFixedThreadPool(10);
executorService = getThreadPool(10, 10);
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,7 +50,7 @@ public class VitessDriverAllowMultiQuerieTest extends TestSuite {

@BeforeClass
public static void init() {
executor = Executors.newFixedThreadPool(10);
executor = getThreadPool(10, 10);
}

@AfterClass
Expand Down

0 comments on commit 4bb12db

Please sign in to comment.