public static boolean waitFor(AtomicBoolean flag, long timeout, TimeUnit unit) throws InterruptedException {
        if (flag == null || unit == null) {
            throw new IllegalArgumentException("Non-null flag and time unit required");
        }

        final long timeoutMs = timeout > 0L ? unit.toMillis(timeout) : 0L;
        final long startTime = timeoutMs < 1L ? 0L : System.currentTimeMillis();
        while (!flag.get()) {
            if (Thread.interrupted()) {
                throw new InterruptedException();
            } else if (startTime > 0L && System.currentTimeMillis() - startTime >= timeoutMs) {
                return false;
            }
        }
        return true;
    }
Waits until the flag turns to {@code true} or timed out.

@param flag    non-null boolean flag to check
@param timeout timeout, negative or zero means forever
@param unit    non-null time unit
@return true if the flag turns to true within given timeout; false otherwise
@throws InterruptedException when thread was interrupted
clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseUtils.java