当前位置: 首页>>代码示例>>Java>>正文


Java TimeUnit.toNanos方法代码示例

本文整理汇总了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();
    }
  }
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:24,代码来源:InterruptionUtil.java

示例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();
}
 
开发者ID:Netflix,项目名称:sstable-adaptor,代码行数:17,代码来源:SimpleCondition.java

示例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();
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:36,代码来源:StampedLock.java

示例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();
    }
}
 
开发者ID:Spencer231,项目名称:GifImageLoader,代码行数:18,代码来源:LinkedBlockingDeque.java

示例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();
    }
}
 
开发者ID:uavorg,项目名称:uavstack,代码行数:20,代码来源:QueueWorkerThreadPoolExecutor.java

示例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;
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:19,代码来源:BoundedPriorityBlockingQueue.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:37,代码来源:StampedLock.java

示例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();
    }
}
 
开发者ID:CableIM,项目名称:Cable-Android,代码行数:23,代码来源:LinkedBlockingDeque.java

示例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();
        }
    }
}
 
开发者ID:tiglabs,项目名称:jsf-sdk,代码行数:21,代码来源:SmoothTokenBucketLimiter.java

示例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();
  }
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:23,代码来源:StealJobQueue.java

示例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);
}
 
开发者ID:oneops,项目名称:oneops,代码行数:9,代码来源:MetricsElasticsearchModule.java

示例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);
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:10,代码来源:ConnectionPool.java

示例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();
    }

}
 
开发者ID:archos-sa,项目名称:aos-MediaLib,代码行数:42,代码来源:ThreadGate.java

示例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);
    }

}
 
开发者ID:StefaniniInspiring,项目名称:pugtsdb,代码行数:30,代码来源:ScheduledThreadPool.java

示例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);
}
 
开发者ID:hortonworks,项目名称:nifi-android-s2s,代码行数:14,代码来源:PropertiesSiteToSiteClientConfigFactory.java


注:本文中的java.util.concurrent.TimeUnit.toNanos方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。