All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hidetake.groovy.ssh.util.ManagedBlocking.groovy Maven / Gradle / Ivy

There is a newer version: 2.11.2
Show newest version
package org.hidetake.groovy.ssh.util

import java.util.concurrent.ForkJoinPool

/**
 * A convenient class of {@link ForkJoinPool.ManagedBlocker}.
 *
 * @author Hidetake Iwata
 */
class ManagedBlocking {
    /**
     * Wait until condition is satisfied.
     *
     * @param intervalMillis polling interval
     * @param condition returns true if polling is completed
     */
    static void until(long intervalMillis = 100L, Closure condition) {
        ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() {
            @Override
            boolean block() throws InterruptedException {
                if (!isReleasable()) {
                    sleep(intervalMillis)
                }
                isReleasable()
            }

            @Override
            boolean isReleasable() {
                condition.call()
            }
        })
    }

    /**
     * Wait given time.
     * @param millis
     */
    static void sleep(long millis) {
        def started = System.currentTimeMillis()
        until {
            (System.currentTimeMillis() - started) > millis
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy