This commit is contained in:
looly 2021-11-24 15:39:31 +08:00
parent fa87bfd01a
commit ff8317dd8c
2 changed files with 22 additions and 5 deletions

View File

@ -2,6 +2,9 @@ package cn.hutool.core.thread;
import cn.hutool.core.date.TimeInterval;
import java.io.Closeable;
import java.io.IOException;
/**
* 高并发测试工具类
*
@ -12,11 +15,14 @@ import cn.hutool.core.date.TimeInterval;
* ct.test(() -> {
* // 需要并发测试的业务代码
* });
*
* Console.log(ct.getInterval());
* ct.close();
* </pre>
*
* @author kwer
*/
public class ConcurrencyTester {
public class ConcurrencyTester implements Closeable {
private final SyncFinisher sf;
private final TimeInterval timeInterval;
private long interval;
@ -31,7 +37,8 @@ public class ConcurrencyTester {
}
/**
* 执行测试
* 执行测试<br>
* 执行测试后不会关闭线程池可以调用{@link #close()}释放线程池
*
* @param runnable 要测试的内容
* @return this
@ -44,8 +51,6 @@ public class ConcurrencyTester {
.addRepeatWorker(runnable)
.setBeginAtSameTime(true)
.start();
// 停止线程池释放资源避免空跑
this.sf.stop();
this.interval = timeInterval.interval();
return this;
@ -76,4 +81,9 @@ public class ConcurrencyTester {
public long getInterval() {
return this.interval;
}
@Override
public void close() throws IOException {
this.sf.close();
}
}

View File

@ -2,6 +2,8 @@ package cn.hutool.core.thread;
import cn.hutool.core.exceptions.UtilException;
import java.io.Closeable;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
@ -25,7 +27,7 @@ import java.util.concurrent.ExecutorService;
* @author Looly
* @since 4.1.15
*/
public class SyncFinisher {
public class SyncFinisher implements Closeable {
private final Set<Worker> workers;
private final int threadSize;
@ -173,6 +175,11 @@ public class SyncFinisher {
return endLatch.getCount();
}
@Override
public void close() throws IOException {
stop();
}
/**
* 工作者为一个线程
*