Implementation:A616567126 GPT WEB JAVA AsyncLockUtils
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Async_Coordination |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete async lock utility using a timed cache and thread pool for coordinating asynchronous operations with timeout-based wait-and-notify semantics.
Description
The AsyncLockUtils class provides a `waitForLock()` method that creates a `LockObject` in a `TimedCache` (24-hour TTL), puts it to sleep in an async thread, and waits for a specified duration for the lock to be awakened. The lock object stores a result code and description, enabling WSS connection callbacks to signal success or failure back to the waiting thread.
Usage
Use this utility when you need to wait for an async operation (like a WSS connection) to complete with a timeout, such as Discord instance connection establishment.
Code Reference
Source Location
- Repository: A616567126_GPT_WEB_JAVA
- File: src/main/java/com/intelligent/bot/utils/mj/AsyncLockUtils.java
- Lines: 1-59
Signature
@UtilityClass
public class AsyncLockUtils {
public static synchronized LockObject getLock(String key);
public static LockObject waitForLock(String key, Duration duration)
throws TimeoutException;
public static class LockObject extends DomainObject {
public LockObject(String id);
// Fields: code, description (from DomainObject)
}
}
Import
import com.intelligent.bot.utils.mj.AsyncLockUtils;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| key | String | Yes | Unique lock identifier |
| duration | Duration | Yes | Maximum wait time |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | LockObject | Lock with result code and description |
Usage Examples
Wait for WSS Connection
import com.intelligent.bot.utils.mj.AsyncLockUtils;
import java.time.Duration;
// Start async WSS connection
instance.startWss();
// Wait up to 10 seconds for connection result
AsyncLockUtils.LockObject lock = AsyncLockUtils.waitForLock(
"wss:" + channelId, Duration.ofSeconds(10));
if (lock.getCode() != ReturnCode.SUCCESS) {
throw new ValidateException(lock.getDescription());
}
// WSS connected successfully