本文整理汇总了Java中java.util.concurrent.TimeUnit.toNanos方法的典型用法代码示例。如果您正苦于以下问题:Java TimeUnit.toNanos方法的具体用法?Java TimeUnit.toNanos怎么用?Java TimeUnit.toNanos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.TimeUnit
的用法示例。
在下文中一共展示了TimeUnit.toNanos方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: joinUninterruptibly
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
private static void joinUninterruptibly(
Thread thread, long timeout, TimeUnit unit) {
boolean interrupted = false;
try {
long remainingNanos = unit.toNanos(timeout);
long end = System.nanoTime() + remainingNanos;
while (true) {
try {
// TimeUnit.timedJoin() treats negative timeouts just like zero.
NANOSECONDS.timedJoin(thread, remainingNanos);
return;
} catch (InterruptedException e) {
interrupted = true;
remainingNanos = end - System.nanoTime();
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
示例2: await
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public boolean await(long time, TimeUnit unit) throws InterruptedException
{
if (isSignaled())
return true;
long start = System.nanoTime();
long until = start + unit.toNanos(time);
if (waiting == null)
waitingUpdater.compareAndSet(this, null, new WaitQueue());
WaitQueue.Signal s = waiting.register();
if (isSignaled())
{
s.cancel();
return true;
}
return s.awaitUntil(until) || isSignaled();
}
示例3: tryReadLock
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* Non-exclusively acquires the lock if it is available within the
* given time and the current thread has not been interrupted.
* Behavior under timeout and interruption matches that specified
* for method {@link Lock#tryLock(long,TimeUnit)}.
*
* @param time the maximum time to wait for the lock
* @param unit the time unit of the {@code time} argument
* @return a stamp that can be used to unlock or convert mode,
* or zero if the lock is not available
* @throws InterruptedException if the current thread is interrupted
* before acquiring the lock
*/
public long tryReadLock(long time, TimeUnit unit)
throws InterruptedException {
long s, m, next, deadline;
long nanos = unit.toNanos(time);
if (!Thread.interrupted()) {
if ((m = (s = state) & ABITS) != WBIT) {
if (m < RFULL) {
if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
return next;
}
else if ((next = tryIncReaderOverflow(s)) != 0L)
return next;
}
if (nanos <= 0L)
return 0L;
if ((deadline = System.nanoTime() + nanos) == 0L)
deadline = 1L;
if ((next = acquireRead(true, deadline)) != INTERRUPTED)
return next;
}
throw new InterruptedException();
}
示例4: pollLast
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public E pollLast(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
E x;
while ((x = unlinkLast()) == null) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
return x;
} finally {
lock.unlock();
}
}
示例5: awaitTermination
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (;;) {
if (runStateAtLeast(ctl.get(), TERMINATED))
return true;
if (nanos <= 0)
return false;
nanos = termination.awaitNanos(nanos);
}
}
finally {
mainLock.unlock();
}
}
示例6: poll
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public E poll(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
lock.lockInterruptibly();
E result = null;
try {
while (queue.size() == 0 && nanos > 0) {
nanos = notEmpty.awaitNanos(nanos);
}
if (queue.size() > 0) {
result = queue.poll();
}
notFull.signal();
} finally {
lock.unlock();
}
return result;
}
示例7: tryReadLock
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* Non-exclusively acquires the lock if it is available within the
* given time and the current thread has not been interrupted.
* Behavior under timeout and interruption matches that specified
* for method {@link Lock#tryLock(long,TimeUnit)}.
*
* @param time the maximum time to wait for the lock
* @param unit the time unit of the {@code time} argument
* @return a read stamp that can be used to unlock or convert mode,
* or zero if the lock is not available
* @throws InterruptedException if the current thread is interrupted
* before acquiring the lock
*/
@ReservedStackAccess
public long tryReadLock(long time, TimeUnit unit)
throws InterruptedException {
long s, m, next, deadline;
long nanos = unit.toNanos(time);
if (!Thread.interrupted()) {
if ((m = (s = state) & ABITS) != WBIT) {
if (m < RFULL) {
if (casState(s, next = s + RUNIT))
return next;
}
else if ((next = tryIncReaderOverflow(s)) != 0L)
return next;
}
if (nanos <= 0L)
return 0L;
if ((deadline = System.nanoTime() + nanos) == 0L)
deadline = 1L;
if ((next = acquireRead(true, deadline)) != INTERRUPTED)
return next;
}
throw new InterruptedException();
}
示例8: offerFirst
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* @throws NullPointerException {@inheritDoc}
* @throws InterruptedException {@inheritDoc}
*/
public boolean offerFirst(E e, long timeout, TimeUnit unit)
throws InterruptedException {
if (e == null) throw new NullPointerException();
Node<E> node = new Node<E>(e);
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (!linkFirst(node)) {
if (nanos <= 0)
return false;
nanos = notFull.awaitNanos(nanos);
}
return true;
} finally {
lock.unlock();
}
}
示例9: uninterruptibleSleep
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
private void uninterruptibleSleep(long sleepTime,TimeUnit unit){
boolean interrupted = false;
try {
long remainingNanos = unit.toNanos(sleepTime);
long end = System.nanoTime() + remainingNanos;
while (true){
try {
NANOSECONDS.sleep(remainingNanos);
return;
} catch (InterruptedException e) {
interrupted = true;
remainingNanos = end - System.nanoTime();
}
}
} finally {
if (interrupted){
Thread.currentThread().interrupt();
}
}
}
示例10: poll
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public T poll(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
lock.lockInterruptibly();
try {
while (true) {
T retVal = this.poll();
if (retVal == null) {
retVal = stealFromQueue.poll();
}
if (retVal == null) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
} else {
return retVal;
}
}
} finally {
lock.unlock();
}
}
示例11: TimerSerializer
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
private TimerSerializer(TimeUnit rateUnit, TimeUnit durationUnit, String timestampFieldname) {
super(JsonTimer.class);
this.timestampFieldname = timestampFieldname;
this.rateUnit = calculateRateUnit(rateUnit, "calls");
this.rateFactor = rateUnit.toSeconds(1);
this.durationUnit = durationUnit.toString().toLowerCase(Locale.US);
this.durationFactor = 1.0 / durationUnit.toNanos(1);
}
示例12: ConnectionPool
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
this.maxIdleConnections = maxIdleConnections;
this.keepAliveDurationNs = timeUnit.toNanos(keepAliveDuration);
// Put a floor on the keep alive duration, otherwise cleanup will spin loop.
if (keepAliveDuration <= 0) {
throw new IllegalArgumentException("keepAliveDuration <= 0: " + keepAliveDuration);
}
}
示例13: pass
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* @return OPEN or BROKEN if gate was passed in specified time. CLOSED if gate didn't
* open in time. And finally InterruptedException in case the thread was interrupted
* during waiting (which also means that the gate was not passed).
*/
public int pass(long time, @NonNull TimeUnit unit) throws InterruptedException {
mLock.lock();
try {
if (mState == CLOSED) {
// even for 0 / negative time.. ?!
waitAction();
// wait for either OPEN or BROKEN, see doc on spurious wakeups why this is in a loop
long remainingNanos = unit.toNanos(time);
while (mState == CLOSED) {
if (remainingNanos <= 0) {
break;
}
remainingNanos = mOpenState.awaitNanos(remainingNanos);
}
}
// not returning mState here since actions can potentially change mState
switch (mState) {
case OPEN:
passOpenAction();
return OPEN;
case BROKEN:
passBrokenAction();
return BROKEN;
case CLOSED:
// no action?
return CLOSED;
default:
throw new IllegalStateException("This is supposed to be unreachable.");
}
} finally {
mLock.unlock();
}
}
示例14: switch
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public ScheduledFuture<?>scheduleAtFixedRate(Runnable command, long initialDelayValue, TimeUnit initialDelayUnit, long periodValue, ChronoUnit periodUnit) {
long initialDelayNanos = initialDelayUnit.toNanos(initialDelayValue);
switch (periodUnit) {
case NANOS:
return scheduleAtFixedRate(command, initialDelayNanos, periodValue, NANOSECONDS);
case MICROS:
return scheduleAtFixedRate(command, initialDelayNanos, TimeUnit.MICROSECONDS.toNanos(periodValue), NANOSECONDS);
case MILLIS:
return scheduleAtFixedRate(command, initialDelayNanos, TimeUnit.MILLISECONDS.toNanos(periodValue), NANOSECONDS);
case SECONDS:
return scheduleAtFixedRate(command, initialDelayNanos, TimeUnit.SECONDS.toNanos(periodValue), NANOSECONDS);
case MINUTES:
return scheduleAtFixedRate(command, initialDelayNanos, TimeUnit.MINUTES.toNanos(periodValue), NANOSECONDS);
case HOURS:
return scheduleAtFixedRate(command, initialDelayNanos, TimeUnit.HOURS.toNanos(periodValue), NANOSECONDS);
default:
Trigger trigger = new ChronoTrigger(periodValue, periodUnit);
Runnable runnable = () -> {
if (trigger.runNow()) {
command.run();
}
};
return scheduleAtFixedRate(runnable, initialDelayNanos, TimeUnit.HOURS.toNanos(1), NANOSECONDS);
}
}
示例15: getDurationNanos
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
protected Long getDurationNanos(Properties properties, String propBase) {
String durationString = StringUtils.emptyToNull(properties.getProperty(propBase));
if (durationString == null) {
return null;
}
long duration = Long.parseLong(durationString);
String unitString = StringUtils.emptyToNull(properties.getProperty(propBase + ".unit"));
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
if (unitString != null) {
timeUnit = TimeUnit.valueOf(unitString);
}
return timeUnit.toNanos(duration);
}