1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00
This commit is contained in:
Looly 2021-05-14 16:58:33 +08:00
parent 415b2a285f
commit 2c1d10d952
3 changed files with 45 additions and 21 deletions
CHANGELOG.md
hutool-core/src
main/java/cn/hutool/core/thread
test/java/cn/hutool/core/util

View File

@ -3,11 +3,12 @@
-------------------------------------------------------------------------------------------------------------
# 5.6.6 (2021-05-11)
# 5.6.6 (2021-05-14)
### 🐣新特性
* 【cron 】 增加时间轮简单实现
* 【core 】 BeanUtil.copyToList增加重载pr#321@Gitee
* 【core 】 SyncFinisher增加stop方法issue#1578@Github
### 🐞Bug修复

View File

@ -11,7 +11,7 @@ import java.util.concurrent.ExecutorService;
/**
* 线程同步结束器<br>
* 在完成一组正在其他线程中执行的操作之前它允许一个或多个线程一直等待
*
*
* <pre>
* ps:
* //模拟1000个线程并发
@ -21,8 +21,8 @@ import java.util.concurrent.ExecutorService;
* });
* sf.start()
* </pre>
*
*
*
*
* @author Looly
* @since 4.1.15
*/
@ -30,7 +30,7 @@ public class SyncFinisher {
private final Set<Worker> workers;
private final int threadSize;
private final ExecutorService executorService;
private ExecutorService executorService;
private boolean isBeginAtSameTime;
/** 启动同步器用于保证所有worker线程同时开始 */
@ -40,19 +40,18 @@ public class SyncFinisher {
/**
* 构造
*
*
* @param threadSize 线程数
*/
public SyncFinisher(int threadSize) {
this.beginLatch = new CountDownLatch(1);
this.threadSize = threadSize;
this.executorService = ThreadUtil.newExecutor(threadSize);
this.workers = new LinkedHashSet<>();
}
/**
* 设置是否所有worker线程同时开始
*
*
* @param isBeginAtSameTime 是否所有worker线程同时开始
* @return this
*/
@ -63,7 +62,7 @@ public class SyncFinisher {
/**
* 增加定义的线程数同等数量的worker
*
*
* @param runnable 工作线程
* @return this
*/
@ -81,7 +80,7 @@ public class SyncFinisher {
/**
* 增加工作线程
*
*
* @param runnable 工作线程
* @return this
*/
@ -96,7 +95,7 @@ public class SyncFinisher {
/**
* 增加工作线程
*
*
* @param worker 工作线程
* @return this
*/
@ -106,20 +105,26 @@ public class SyncFinisher {
}
/**
* 开始工作
* 开始工作<br>
* 执行此方法后如果不再重复使用此对象需调用{@link #stop()}关闭回收资源
*/
public void start() {
start(true);
}
/**
* 开始工作
*
* 开始工作<br>
* 执行此方法后如果不再重复使用此对象需调用{@link #stop()}关闭回收资源
*
* @param sync 是否阻塞等待
* @since 4.5.8
*/
public void start(boolean sync) {
endLatch = new CountDownLatch(workers.size());
if(null == this.executorService || this.executorService.isShutdown()){
this.executorService = ThreadUtil.newExecutor(threadSize);
}
for (Worker worker : workers) {
executorService.submit(worker);
}
@ -135,9 +140,27 @@ public class SyncFinisher {
}
}
/**
* 结束线程池此方法执行两种情况
* <ol>
* <li>执行start(true)调用此方法结束线程池回收资源</li>
* <li>执行start(false)用户自行判断结束点执行此方法</li>
* </ol>
*
* @since 5.6.6
*/
public void stop(){
if(null != this.executorService){
this.executorService.shutdown();
}
this.executorService = null;
clearWorker();
}
/**
* 等待所有Worker工作结束否则阻塞
*
*
* @throws InterruptedException 用户中断
* @deprecated 使用start方法指定是否阻塞等待
*/
@ -159,7 +182,7 @@ public class SyncFinisher {
/**
* 剩余任务数
*
*
* @return 剩余任务数
*/
public long count() {
@ -168,7 +191,7 @@ public class SyncFinisher {
/**
* 工作者为一个线程
*
*
* @author xiaoleilu
*
*/

View File

@ -11,25 +11,25 @@ import java.util.List;
import java.util.Set;
public class RandomUtilTest {
@Test
public void randomEleSetTest(){
Set<Integer> set = RandomUtil.randomEleSet(CollUtil.newArrayList(1, 2, 3, 4, 5, 6), 2);
Assert.assertEquals(set.size(), 2);
}
@Test
public void randomElesTest(){
List<Integer> result = RandomUtil.randomEles(CollUtil.newArrayList(1, 2, 3, 4, 5, 6), 2);
Assert.assertEquals(result.size(), 2);
}
@Test
public void randomDoubleTest() {
double randomDouble = RandomUtil.randomDouble(0, 1, 0, RoundingMode.HALF_UP);
Assert.assertTrue(randomDouble <= 1);
}
@Test
@Ignore
public void randomBooleanTest() {