Skip to content

Implement a LatchUtils helper class to wrap CountDownLatch instantiation along with countDown() and await() method invocations, decoupling business logic from concurrency control for safer and cleaner thread coordination #1394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/main/java/org/apache/commons/lang3/concurrent/LatchUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.apache.commons.lang3.concurrent;


import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/**
* the utility class for {@link CountDownLatch}
* Provide a safer, more user-friendly, and concise wrapper for CountDownLatch's creation, countDown(), and await() operations
* To achieve separation between business logic and CountDownLatch
*/
public class LatchUtils {

private static final ThreadLocal<List<TaskInfo>> THREADLOCAL = ThreadLocal.withInitial(LinkedList::new);

private static final class TaskInfo {
private Executor executor;
private Runnable runnable;

public TaskInfo(Executor executor, Runnable runnable) {
this.executor = executor;
this.runnable = runnable;
}
}

/**
* @param executor the threadpool for the task will run in
* @param runnable the task
*/
public static void submitTask(Executor executor, Runnable runnable) {
THREADLOCAL.get().add(new TaskInfo(executor, runnable));
}

private static List<TaskInfo> popTask() {
List<TaskInfo> taskInfos = THREADLOCAL.get();
THREADLOCAL.remove();
return taskInfos;
}

/**
* start task and wait for task finish
*
* @param timeout
* @return
*/
public static boolean waitFor(Long timeout, TimeUnit timeUnit) {
List<TaskInfo> taskInfos = popTask();
if (taskInfos.isEmpty()) {
return true;
}

CountDownLatch latch = new CountDownLatch(taskInfos.size());
for (TaskInfo taskInfo : taskInfos) {
Executor executor = taskInfo.executor;
Runnable runnable = taskInfo.runnable;
executor.execute(() -> {
try {
runnable.run();
} finally {
latch.countDown();
}
});
}

boolean await = false;
try {
if (timeout != null) {
await = latch.await(timeout, timeUnit != null ? timeUnit : TimeUnit.SECONDS);
} else {
latch.await();
await = true;
}
} catch (Exception e) {

}

return await;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.apache.commons.lang3.concurrent;

import org.apache.commons.lang3.AbstractLangTest;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests {@link LatchUtils}.
*/
class LatchUtilsTest extends AbstractLangTest {

private ExecutorService executorService = Executors.newFixedThreadPool(2);

@Test
void testSubmitTask1() {
LatchUtils.submitTask(executorService, () -> {
System.out.println("task1");
});
LatchUtils.submitTask(executorService, () -> {
System.out.println("task2");

});
LatchUtils.submitTask(executorService, () -> {
System.out.println("task3");

});
assertTrue(LatchUtils.waitFor(1L, TimeUnit.SECONDS
));
}
}