cn.geektool.core.thread.ConcurrencyTester Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geektool-core Show documentation
Show all versions of geektool-core Show documentation
geektool核心,包括集合、字符串、Bean等工具
The newest version!
package cn.geektool.core.thread;
import cn.geektool.core.date.TimeInterval;
/**
* 高并发测试工具类
*
*
* ps:
* //模拟1000个线程并发
* ConcurrencyTester ct = new ConcurrencyTester(1000);
* ct.test(() -> {
* // 需要并发测试的业务代码
* });
*
*
* @author kwer
*/
public class ConcurrencyTester {
private final SyncFinisher sf;
private final TimeInterval timeInterval;
private long interval;
/**
* 构造
* @param threadSize 线程数
*/
public ConcurrencyTester(int threadSize) {
this.sf = new SyncFinisher(threadSize);
this.timeInterval = new TimeInterval();
}
/**
* 执行测试
*
* @param runnable 要测试的内容
* @return this
*/
public ConcurrencyTester test(Runnable runnable) {
timeInterval.start();
this.sf//
.addRepeatWorker(runnable)//
.setBeginAtSameTime(true)// 同时开始
.start();
this.interval = timeInterval.interval();
return this;
}
/**
* 获取执行时间
*
* @return 执行时间,单位毫秒
*/
public long getInterval() {
return this.interval;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy