Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A616567126 GPT WEB JAVA AsyncLockUtils

From Leeroopedia
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

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

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment