本文整理汇总了Java中java.util.concurrent.TimeUnit.convert方法的典型用法代码示例。如果您正苦于以下问题:Java TimeUnit.convert方法的具体用法?Java TimeUnit.convert怎么用?Java TimeUnit.convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.TimeUnit
的用法示例。
在下文中一共展示了TimeUnit.convert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: lockInterruptibly
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
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;
}
}
}
示例2: to
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
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");
}
}
}
示例3: getDelay
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* 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);
}
开发者ID:PacktPublishing,项目名称:Java-SE-9-Road-to-Concurrent-and-High-Performance-Programming,代码行数:10,代码来源:Event.java
示例4: parse
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
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);
}
}
示例5: getKeepAliveDelay
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public long getKeepAliveDelay(TimeUnit unit) {
return unit.convert(keepAliveDelayNanos, TimeUnit.NANOSECONDS);
}
示例6: getDelay
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public long getDelay(TimeUnit unit)
{
return unit.convert(trigger - System.nanoTime(), NANOSECONDS);
}
示例7: getMaxTransactionTime
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public long getMaxTransactionTime(TimeUnit timeUnit) {
return timeUnit.convert(maxTransactionTimeMillis, TimeUnit.MILLISECONDS);
}
示例8: getTimeout
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public long getTimeout(final TimeUnit timeUnit) {
return timeUnit.convert(sslSocketChannel.getTimeout(), TimeUnit.MILLISECONDS);
}
示例9: get
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@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();
}
示例10: getTimeout
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public long getTimeout(final TimeUnit timeUnit) {
return timeUnit.convert(timeoutMillis, TimeUnit.MILLISECONDS);
}
示例11: now
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
default long now(TimeUnit unit) {
return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
示例12: getDelay
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public long getDelay(@NonNull TimeUnit timeUnit) {
return timeUnit.convert(delayInMillis - (System.currentTimeMillis() - origin), TimeUnit.MILLISECONDS);
}
示例13: tryLock
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
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;
}
示例14: getDelay
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/** 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);
}
示例15: getDelay
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public long getDelay(TimeUnit unit) {
return unit.convert(this.expirationTime - EnvironmentEdgeManager.currentTime(),
TimeUnit.MILLISECONDS);
}