Example usage for java.util.concurrent.locks LockSupport parkUntil

List of usage examples for java.util.concurrent.locks LockSupport parkUntil

Introduction

In this page you can find the example usage for java.util.concurrent.locks LockSupport parkUntil.

Prototype

public static void parkUntil(long deadline) 

Source Link

Document

Disables the current thread for thread scheduling purposes, until the specified deadline, unless the permit is available.

Usage

From source file:com.pushtechnology.consulting.SessionCreator.java

/**
 * Session churn.//from  w  w w.jav a2s . c  o m
 *
 * @param multiIpClientAddresses
 * @param sessionCreateRatePerSec
 * @param sessionDurationMs
 */
private void doStart(long sessionCreateRatePerSec, long sessionDurationSec) {

    LOG.trace("SessionCreator#doStart for '{}' sessions/second and '{}' sessionDurationMs",
            sessionCreateRatePerSec, sessionDurationSec);

    final long interval = 1000 / sessionCreateRatePerSec;
    long now = System.currentTimeMillis();

    do {
        try {
            startedSessions.incrementAndGet();
            Benchmarker.connectThreadPool.submit(new Runnable() {

                @Override
                public void run() {

                    LOG.trace("Adding session");
                    try {
                        /* ASYNC session creation */
                        sessionFactory.open(getConnectionString(),
                                new OpenChurningSessionCallback(sessionDurationSec));
                        LOG.trace("Done submitting session open");
                    } catch (Exception e) {
                        /* ASYNC session creation */
                        connectionFailures.incrementAndGet();
                        LOG.error("Exception caught trying to connect:", e);
                    }
                }

                private String getConnectionString() {
                    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    return connectionStrings.get(rnd.nextInt(connectionStrings.size()));
                }
            });
        } catch (Exception e) {
            LOG.error("Exception caught when submitting session open ", e.getMessage());
            connectionFailures.incrementAndGet();
        }
        if (connectedSessions.get() >= sessionCreateRatePerSec * sessionDurationSec) {
            writeSteadyStateFlagFile();
        }

        now = now + interval;
        LockSupport.parkUntil(now);
    } while (true);
}