Java Code Examples for java.util.concurrent.TimeUnit.convert()

The following are Jave code examples for showing how to use convert() of the java.util.concurrent.TimeUnit class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: JRediClients   File: RedissonMultiLock.java   Source Code and License Vote up 6 votes
public void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException {
    long waitTime = -1;
    if (leaseTime == -1) {
        waitTime = 5;
        unit = TimeUnit.SECONDS;
    } else {
        waitTime = unit.toMillis(leaseTime);
        if (waitTime <= 2000) {
            waitTime = 2000;
        } else if (waitTime <= 5000) {
            waitTime = ThreadLocalRandom.current().nextLong(waitTime/2, waitTime);
        } else {
            waitTime = ThreadLocalRandom.current().nextLong(5000, waitTime);
        }
        waitTime = unit.convert(waitTime, TimeUnit.MILLISECONDS);
    }
    
    while (true) {
        if (tryLock(waitTime, leaseTime, unit)) {
            return;
        }
    }
}
 
Example 2
Project: jdk8u-jdk   File: Basic.java   Source Code and License Vote up 5 votes
static void to(long v, TimeUnit unit) {
    FileTime t = FileTime.from(v, unit);
    for (TimeUnit u: TimeUnit.values()) {
        long result = t.to(u);
        long expected = u.convert(v, unit);
        if (result != expected) {
            throw new RuntimeException("unexpected result");
        }
    }
}
 
Example 3
Project: Java-SE-9-Road-to-Concurrent-and-High-Performance-Programming   File: Event.java   Source Code and License Vote up 5 votes
/**
 * Method that returns the remaining time to the activation of the event
 */
@Override
public long getDelay(TimeUnit unit) {
	Date now = new Date();
	long diff = startDate.getTime() - now.getTime();
	return unit.convert(diff, TimeUnit.MILLISECONDS);
}
 
Example 4
Project: serverless-cf-analysis   File: TimeUnitParser.java   Source Code and License Vote up 5 votes
public static long parse(String value, TimeUnit timeUnit) {
    try {
        return timeUnit.convert(Long.parseLong(value), TimeUnit.MILLISECONDS);
    } catch (NumberFormatException e) {
        long millis = PARSER.parsePeriod(value.replace(" ", "")).toStandardDuration().getMillis();
        return timeUnit.convert(millis, TimeUnit.MILLISECONDS);
    }
}
 
Example 5
Project: fdt   File: Config.java   Source Code and License Vote up 4 votes
public long getKeepAliveDelay(TimeUnit unit) {
    return unit.convert(keepAliveDelayNanos, TimeUnit.NANOSECONDS);
}
 
Example 6
Project: think-in-java   File: DelayQueueDemo.java   Source Code and License Vote up 4 votes
@Override
public long getDelay(TimeUnit unit)
{
    return unit.convert(trigger - System.nanoTime(), NANOSECONDS);
}
 
Example 7
Project: nifi-android-s2s   File: QueuedSiteToSiteClientConfig.java   Source Code and License Vote up 4 votes
public long getMaxTransactionTime(TimeUnit timeUnit) {
    return timeUnit.convert(maxTransactionTimeMillis, TimeUnit.MILLISECONDS);
}
 
Example 8
Project: nifi-nars   File: SSLCommsSession.java   Source Code and License Vote up 4 votes
@Override
public long getTimeout(final TimeUnit timeUnit) {
    return timeUnit.convert(sslSocketChannel.getTimeout(), TimeUnit.MILLISECONDS);
}
 
Example 9
Project: voyage   File: FutureAdapter.java   Source Code and License Vote up 4 votes
@Override
public V get(long timeout, TimeUnit unit) throws InterruptedException,
		ExecutionException, TimeoutException {
	int timeoutInMillis = (int) unit.convert(timeout, TimeUnit.MILLISECONDS);
	return (V)invokeFuture.get(timeoutInMillis).getResult();
}
 
Example 10
Project: think-in-java   File: E33_GreenhouseController.java   Source Code and License Vote up 4 votes
@Override
public long getDelay(TimeUnit unit)
{
    return unit.convert(trigger - System.nanoTime(), NANOSECONDS);
}
 
Example 11
Project: nifi-nars   File: StandardCommsSession.java   Source Code and License Vote up 4 votes
@Override
public long getTimeout(final TimeUnit timeUnit) {
    return timeUnit.convert(timeoutMillis, TimeUnit.MILLISECONDS);
}
 
Example 12
Project: Reactive4JavaFlow   File: SchedulerService.java   Source Code and License Vote up 4 votes
default long now(TimeUnit unit) {
    return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
 
Example 13
Project: habpanelviewer   File: Average.java   Source Code and License Vote up 4 votes
@Override
public long getDelay(@NonNull TimeUnit timeUnit) {
    return timeUnit.convert(delayInMillis - (System.currentTimeMillis() - origin), TimeUnit.MILLISECONDS);
}
 
Example 14
Project: JRediClients   File: RedissonMultiLock.java   Source Code and License Vote up 4 votes
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
    long newLeaseTime = -1;
    if (leaseTime != -1) {
        newLeaseTime = waitTime*2;
    }
    
    long time = System.currentTimeMillis();
    long remainTime = -1;
    if (waitTime != -1) {
        remainTime = unit.toMillis(waitTime);
    }
    int failedLocksLimit = failedLocksLimit();
    List<RLock> lockedLocks = new ArrayList<RLock>(locks.size());
    for (ListIterator<RLock> iterator = locks.listIterator(); iterator.hasNext();) {
        RLock lock = iterator.next();
        boolean lockAcquired;
        try {
            if (waitTime == -1 && leaseTime == -1) {
                lockAcquired = lock.tryLock();
            } else {
                long awaitTime = unit.convert(remainTime, TimeUnit.MILLISECONDS);
                lockAcquired = lock.tryLock(awaitTime, newLeaseTime, unit);
            }
        } catch (Exception e) {
            lockAcquired = false;
        }
        
        if (lockAcquired) {
            lockedLocks.add(lock);
        } else {
            if (locks.size() - lockedLocks.size() == failedLocksLimit()) {
                break;
            }

            if (failedLocksLimit == 0) {
                unlockInner(lockedLocks);
                if (waitTime == -1 && leaseTime == -1) {
                    return false;
                }
                failedLocksLimit = failedLocksLimit();
                lockedLocks.clear();
                // reset iterator
                while (iterator.hasPrevious()) {
                    iterator.previous();
                }
            } else {
                failedLocksLimit--;
            }
        }
        
        if (remainTime != -1) {
            remainTime -= (System.currentTimeMillis() - time);
            time = System.currentTimeMillis();
            if (remainTime <= 0) {
                unlockInner(lockedLocks);
                return false;
            }
        }
    }

    if (leaseTime != -1) {
        List<RFuture<Boolean>> futures = new ArrayList<RFuture<Boolean>>(lockedLocks.size());
        for (RLock rLock : lockedLocks) {
            RFuture<Boolean> future = rLock.expireAsync(unit.toMillis(leaseTime), TimeUnit.MILLISECONDS);
            futures.add(future);
        }
        
        for (RFuture<Boolean> rFuture : futures) {
            rFuture.syncUninterruptibly();
        }
    }
    
    return true;
}
 
Example 15
Project: hadoop-oss   File: DelegationTokenRenewer.java   Source Code and License Vote up 4 votes
/** Get the delay until this event should happen. */
@Override
public long getDelay(final TimeUnit unit) {
  final long millisLeft = renewalTime - Time.now();
  return unit.convert(millisLeft, TimeUnit.MILLISECONDS);
}
 
Example 16
Project: ditb   File: Leases.java   Source Code and License Vote up 4 votes
public long getDelay(TimeUnit unit) {
  return unit.convert(this.expirationTime - EnvironmentEdgeManager.currentTime(),
      TimeUnit.MILLISECONDS);
}
 
Example 17
Project: hadoop   File: StopWatch.java   Source Code and License Vote up 4 votes
/**
 * @return current elapsed time in specified timeunit.
 */
public long now(TimeUnit timeUnit) {
  return timeUnit.convert(now(), TimeUnit.NANOSECONDS);

}
 
Example 18
Project: sstore-soft   File: Stopwatch.java   Source Code and License Vote up 2 votes
/**
 * Returns the current elapsed time shown on this stopwatch, expressed
 * in the desired time unit, with any fraction rounded down.
 *
 * <p>Note that the overhead of measurement can be more than a microsecond, so
 * it is generally not useful to specify {@link TimeUnit#NANOSECONDS}
 * precision here.
 *
 * @since 14.0 (since 10.0 as {@code elapsedTime()})
 */
public long elapsed(TimeUnit desiredUnit) {
  return desiredUnit.convert(elapsedNanos(), NANOSECONDS);
}
 
Example 19
Project: googles-monorepo-demo   File: Stopwatch.java   Source Code and License Vote up 2 votes
/**
 * Returns the current elapsed time shown on this stopwatch, expressed in the desired time unit,
 * with any fraction rounded down.
 *
 * <p>Note that the overhead of measurement can be more than a microsecond, so it is generally not
 * useful to specify {@link TimeUnit#NANOSECONDS} precision here.
 *
 * @since 14.0 (since 10.0 as {@code elapsedTime()})
 */
public long elapsed(TimeUnit desiredUnit) {
  return desiredUnit.convert(elapsedNanos(), NANOSECONDS);
}
 
Example 20
Project: aos-MediaLib   File: BaseTags.java   Source Code and License Vote up 0 votes
public long getRuntime(TimeUnit unit) { return unit.convert(mRuntimeMs, TimeUnit.MILLISECONDS); }