闭锁、信号量
闭锁
CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。
public class CountDownLatchTest {
private int threadNum = 5;//执行任务的子线程数量
private int workNum = 20;//任务数量
private ExecutorService service;
private ArrayBlockingQueue<String> blockingQueue;
private CountDownLatch latch;
@Before
public void setUp() {
service = Executors.newFixedThreadPool(threadNum, new ThreadFactoryBuilder().setNameFormat("WorkThread-%d").build());
blockingQueue = new ArrayBlockingQueue<>(workNum);
for (int i = 0; i < workNum; i++) {
blockingQueue.add("任务-" + i);
}
latch = new CountDownLatch(workNum);//计数器的值为任务的数量
}
@Test
public void test() throws InterruptedException {
SoutUtil.print("主线程开始运行");
for (int i = 0; i < workNum; i++) {
service.execute(new WorkRunnable());
}
latch.await();//等待子线程的所有任务完成
SoutUtil.print("主线程去做其它事");
}
//用blockQueue中的元素模拟任务
public String getWork() {
return blockingQueue.poll();
}
class WorkRunnable implements Runnable {
public void run() {
String work = getWork();
performWork(work);
latch.countDown();//完成一个任务就调用一次
}
}
private void performWork(String work) {
SoutUtil.print("处理任务:" + work);
try {
//模拟耗时的任务
Thread.currentThread().sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
信号量
Semaphore可以用来控制同时访问特定资源的线程数量,通过协调各个线程,以保证合理的使用资源。 Semaphore可以用来做流量分流,特别是对公共资源有限的场景,比如数据库连接。
public class SemaphoreTest {
private static final int COUNT = 40;
private static Executor executor = Executors.newFixedThreadPool(COUNT);
private static Semaphore semaphore = new Semaphore(10);
public static void main(String[] args) {
for (int i=0; i< COUNT; i++) {
executor.execute(new ThreadTest.Task());
}
}
static class Task implements Runnable {
@Override
public void run() {
try {
//读取文件操作
semaphore.acquire();
// 存数据过程
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
}
}
Last updated