com.base4j.util.lang.SyncFinisher Maven / Gradle / Ivy
The newest version!
package com.base4j.util.lang;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import com.base4j.util.exceptions.NotInitedException;
/**
* 线程同步结束器
* 在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
* 不能保证同时开始
*
* @author xxx
*/
public class SyncFinisher {
private CountDownLatch countDownLatch;
@SuppressWarnings("MapOrSetKeyShouldOverrideHashCodeEquals")
private Set workers = new LinkedHashSet();
/**
* 增加工作线程
*
* @param worker 工作线程
*/
synchronized public void addWorker(AbstractWorker worker) {
workers.add(worker);
}
/**
* 开始工作
*/
public void start() {
countDownLatch = new CountDownLatch(workers.size());
for (AbstractWorker worker : workers) {
worker.start();
}
}
/**
* 等待所有Worker工作结束,否则阻塞
*
* @throws InterruptedException
*/
public void await() throws InterruptedException {
if (countDownLatch == null) {
throw new NotInitedException("Please call start() method first!");
}
countDownLatch.await();
}
/**
* 清空工作线程对象
*/
public void clearWorker() {
workers.clear();
}
/**
* @return 并发数
*/
public long count() {
return countDownLatch.getCount();
}
/**
* 工作者,为一个线程
*
* @author xxx
*/
public abstract class AbstractWorker extends Thread {
@Override
public void run() {
try {
work();
} finally {
countDownLatch.countDown();
}
}
public abstract void work();
}
}