mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
fa87bfd01a
commit
ff8317dd8c
@ -2,6 +2,9 @@ package cn.hutool.core.thread;
|
|||||||
|
|
||||||
import cn.hutool.core.date.TimeInterval;
|
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(() -> {
|
* ct.test(() -> {
|
||||||
* // 需要并发测试的业务代码
|
* // 需要并发测试的业务代码
|
||||||
* });
|
* });
|
||||||
|
*
|
||||||
|
* Console.log(ct.getInterval());
|
||||||
|
* ct.close();
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author kwer
|
* @author kwer
|
||||||
*/
|
*/
|
||||||
public class ConcurrencyTester {
|
public class ConcurrencyTester implements Closeable {
|
||||||
private final SyncFinisher sf;
|
private final SyncFinisher sf;
|
||||||
private final TimeInterval timeInterval;
|
private final TimeInterval timeInterval;
|
||||||
private long interval;
|
private long interval;
|
||||||
@ -31,7 +37,8 @@ public class ConcurrencyTester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行测试
|
* 执行测试<br>
|
||||||
|
* 执行测试后不会关闭线程池,可以调用{@link #close()}释放线程池
|
||||||
*
|
*
|
||||||
* @param runnable 要测试的内容
|
* @param runnable 要测试的内容
|
||||||
* @return this
|
* @return this
|
||||||
@ -44,8 +51,6 @@ public class ConcurrencyTester {
|
|||||||
.addRepeatWorker(runnable)
|
.addRepeatWorker(runnable)
|
||||||
.setBeginAtSameTime(true)
|
.setBeginAtSameTime(true)
|
||||||
.start();
|
.start();
|
||||||
// 停止线程池释放资源,避免空跑
|
|
||||||
this.sf.stop();
|
|
||||||
|
|
||||||
this.interval = timeInterval.interval();
|
this.interval = timeInterval.interval();
|
||||||
return this;
|
return this;
|
||||||
@ -76,4 +81,9 @@ public class ConcurrencyTester {
|
|||||||
public long getInterval() {
|
public long getInterval() {
|
||||||
return this.interval;
|
return this.interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
this.sf.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package cn.hutool.core.thread;
|
|||||||
|
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
@ -25,7 +27,7 @@ import java.util.concurrent.ExecutorService;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 4.1.15
|
* @since 4.1.15
|
||||||
*/
|
*/
|
||||||
public class SyncFinisher {
|
public class SyncFinisher implements Closeable {
|
||||||
|
|
||||||
private final Set<Worker> workers;
|
private final Set<Worker> workers;
|
||||||
private final int threadSize;
|
private final int threadSize;
|
||||||
@ -173,6 +175,11 @@ public class SyncFinisher {
|
|||||||
return endLatch.getCount();
|
return endLatch.getCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工作者,为一个线程
|
* 工作者,为一个线程
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user