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

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

Introduction

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

Prototype

public static void parkNanos(long nanos) 

Source Link

Document

Disables the current thread for thread scheduling purposes, for up to the specified waiting time, unless the permit is available.

Usage

From source file:com.alibaba.otter.node.etl.common.jmx.StageAggregationTest.java

@Test
public void test_normal() {
    StageAggregation aggregation = new StageAggregation(256);

    for (int i = 0; i < 128; i++) {
        long now = System.currentTimeMillis();
        aggregation.push(new AggregationItem(now - 10 - RandomUtils.nextInt(100), now));
        LockSupport.parkNanos(1000 * 1000L);
    }//  w ww.  ja v a2 s .c o  m

    LockSupport.parkNanos(2000 * 1000 * 1000L);

    for (int i = 0; i < 200; i++) {
        long now = System.currentTimeMillis();
        aggregation.push(new AggregationItem(now - 10 - RandomUtils.nextInt(100), now));
        LockSupport.parkNanos(1000 * 1000L);
    }

    String result = aggregation.histogram();
    System.out.println(result);
}

From source file:org.neo4j.kernel.TokenCreationIT.java

@Test
@RepeatRule.Repeat(times = 5)/*from www .  j  a v a  2s.  c o  m*/
public void concurrentLabelTokenCreation() throws InterruptedException {
    int concurrentWorkers = 10;
    CountDownLatch latch = new CountDownLatch(concurrentWorkers);
    for (int i = 0; i < concurrentWorkers; i++) {
        new LabelCreator(databaseRule, latch).start();
    }
    LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(500));
    stop = true;
    latch.await();
}

From source file:com.hazelcast.simulator.worker.metronome.SleepingMetronome.java

@Override
public long waitForNext() {
    // set random interval on the first run
    if (nextNanos == 0) {
        nextNanos = nanoTime() + nextLong(0, intervalNanos);
    }/*w ww.j  a v a2s. c o  m*/

    long now;
    while ((now = System.nanoTime()) < nextNanos) {
        LockSupport.parkNanos(nextNanos - now);
    }

    long expectedStartNanos = nextNanos;
    nextNanos += intervalNanos;
    return accountForCoordinatedOmission ? expectedStartNanos : nanoTime();
}

From source file:org.nd4j.parameterserver.distributed.v2.transport.impl.DelayedDummyTransport.java

@Override
public void sendMessage(@NonNull VoidMessage message, @NonNull String id) {
    val bos = new ByteArrayOutputStream();
    synchronized (this) {
        SerializationUtils.serialize(message, bos);
    }/*from  www. j a v  a  2s  .com*/

    val bis = new ByteArrayInputStream(bos.toByteArray());
    final VoidMessage msg = SerializationUtils.deserialize(bis);

    if (msg.getOriginatorId() == null)
        msg.setOriginatorId(this.id());

    //super.sendMessage(message, id);
    connector.executorService().submit(new Runnable() {
        @Override
        public void run() {
            try {
                // imitate some bad network here, latency of 0.2ms - 0.7ms
                val sleepTime = RandomUtils.nextInt(200, 700) * 1000;
                LockSupport.parkNanos(sleepTime);

                DelayedDummyTransport.super.sendMessage(msg, id);
            } catch (Exception e) {
                //
            }
        }
    });
}

From source file:com.alibaba.otter.manager.biz.common.alarm.AbstractAlarmService.java

public void afterPropertiesSet() throws Exception {
    executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                sendAlarmInternal();//  w  ww  .j av  a  2 s  . co m
                LockSupport.parkNanos(period * 1000L * 1000L);
            }
        }
    });
}

From source file:com.googlecode.concurrentlinkedhashmap.MemoryLeakTest.java

@Test
public void memoryLeak() throws InterruptedException {
    timeTasks(threads, new Runnable() {
        @Override/*from  w ww  .java 2 s . c  o m*/
        public void run() {
            Long id = Thread.currentThread().getId();
            map.put(id, id);
            for (;;) {
                map.get(id);
                Thread.yield();
                LockSupport.parkNanos(1L);
            }
        }
    });
}

From source file:com.ottogroup.bi.asap.component.strategy.SleepingWaitStrategy.java

/**
 * Waiting .... //from   ww  w.  ja  v  a2 s . c om
 * @param counter
 * @return
 */
private int applyWaitMethod(int counter) {
    if (counter > 100) {
        --counter;
    } else {
        LockSupport.parkNanos(1L);
    }
    return counter;
}

From source file:oz.hadoop.yarn.api.core.CommandProcessLauncher.java

/**
 * /*w w w .  j  ava2s  .c o  m*/
 */
@Override
public void finish() {
    if (!((boolean) ReflectionUtils.getFieldValue(this.process, "hasExited"))) {
        this.process.destroy();
    }
    while (!((boolean) ReflectionUtils.getFieldValue(this.process, "hasExited"))) {
        LockSupport.parkNanos(1000);
    }
    int exitCode = this.process.exitValue();
    logger.info("Command finished with exit code: " + exitCode);
    super.finish();
}

From source file:SimpleTimer.java

/**
 * If !isExpired(), wait for time remaining, or until the thread is
 * unparked. Time remaining is calculated as follows:
 * <p>//from w  w  w.ja  v a  2  s  . co m
 * <ol>
 * <li>If timeToWait is -1, wait unconditionally.</li>
 * <li>Otherwise, timeToWait is calculated as the amount of time originally
 * requested minus the time already waited.</li>
 * </ol>
 */
public void await() {
    if (expired) {
        return;
    }
    if (timeToWait == -1) {
        LockSupport.park();
        expired = true;
        return;
    }
    long now = System.nanoTime();
    long timeWaited = now - startTime;
    if (timeWaited >= timeToWait) {
        expired = true;
        return;
    }
    long timeToWaitThisTime = timeToWait - timeWaited;
    assert timeToWaitThisTime > 0 && timeToWaitThisTime <= timeToWait;
    LockSupport.parkNanos(timeToWaitThisTime);
}

From source file:org.fastmongo.odm.dbobject.mapping.core.DbObjectToDomainConverterTest.java

@Test
public void testGarbageCollectedRootObjects() {
    DBObject db;// ww  w.j ava  2 s  .  co m
    {
        Group root = TestDomainObjectsFixtures.createGroup(5, 10);
        db = toDbConverter.toDbObject(root);
    }

    Group root = fromDbConverter.fromDbObject(db, Group.class);
    Reference<Group> ref = new WeakReference<>(root);
    List<User> list = root.getUsers();

    Assert.assertNotNull(ref.get());

    //noinspection UnusedAssignment
    root = null;

    Runtime.getRuntime().gc();

    int limit = 10;
    int wait = 0;
    while (ref.get() != null && wait++ < limit) {
        if (ref.get() == null) {
            break;
        }

        LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); // wait a bit
    }

    Assert.assertNull(ref.get());

    // touch proxy, it should not throw NPE as before
    Assert.assertTrue(!list.isEmpty());
}