Skip to content

Commit

Permalink
fix: Added ThreadUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed May 16, 2024
1 parent fd9fdfd commit 91bcd7b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -38,6 +37,7 @@
import com.tsurugidb.benchmark.costaccounting.util.BenchConst.BatchFactoryOrder;
import com.tsurugidb.benchmark.costaccounting.util.BenchConst.IsolationLevel;
import com.tsurugidb.benchmark.costaccounting.util.BenchRandom;
import com.tsurugidb.benchmark.costaccounting.util.ThreadUtil;
import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting;
import com.tsurugidb.iceaxe.transaction.option.TgTxOption;
import com.tsurugidb.iceaxe.transaction.option.TgTxOptionLtx;
Expand Down Expand Up @@ -339,7 +339,7 @@ private int executeParallel(List<? extends Callable<Void>> threadList) {

int threadPoolSize = config.getThreadSize();
LOG.info("threadPoolSize={}", threadPoolSize);
ExecutorService service = Executors.newFixedThreadPool(threadPoolSize);
ExecutorService service = ThreadUtil.newFixedThreadPool("CostAccountingBatch.thread-", threadPoolSize);

List<Future<Void>> resultList = Collections.emptyList();
try {
Expand Down Expand Up @@ -425,7 +425,7 @@ private int executeQueue() {
});

int size = 8;
ExecutorService service = Executors.newFixedThreadPool(size);
ExecutorService service = ThreadUtil.newFixedThreadPool("CostAccountingBatch.thread-", size);
threadList.clear();
List<Callable<Void>> list = Stream.generate(() -> new Callable<Void>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Stream;

Expand All @@ -26,6 +25,7 @@
import com.tsurugidb.benchmark.costaccounting.online.OnlineConfig;
import com.tsurugidb.benchmark.costaccounting.util.BenchConst;
import com.tsurugidb.benchmark.costaccounting.util.BenchConst.SqlDistinct;
import com.tsurugidb.benchmark.costaccounting.util.ThreadUtil;
import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting;
import com.tsurugidb.iceaxe.transaction.option.TgTxOption;

Expand All @@ -51,7 +51,8 @@ public BenchPeriodicUpdateStockTask(int taskId) {
if (threadSize <= 1) {
this.service = null;
} else {
this.service = Executors.newFixedThreadPool(threadSize);
String threadName = String.format("%s.%d.thread-", TASK_NAME, taskId);
this.service = ThreadUtil.newFixedThreadPool(threadName, threadSize);
}
this.keepSize = BenchConst.periodicKeepSize(TASK_NAME);
LOG.info("keep.size={}", keepSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.tsurugidb.benchmark.costaccounting.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NameThreadFactory implements ThreadFactory {

private final String baseName;

private final AtomicInteger number = new AtomicInteger(0);

public NameThreadFactory(String baseName) {
this.baseName = baseName;
}

@Override
public Thread newThread(Runnable r) {
var thread = new Thread(r);
thread.setName(baseName + number.getAndIncrement());
return thread;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.tsurugidb.benchmark.costaccounting.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadUtil {

public static ExecutorService newFixedThreadPool(String baseName, int threads) {
var factory = new NameThreadFactory(baseName);
return Executors.newFixedThreadPool(threads, factory);
}
}

0 comments on commit 91bcd7b

Please sign in to comment.