本文整理了Java中com.google.common.util.concurrent.Monitor.toSafeNanos()
方法的一些代码示例,展示了Monitor.toSafeNanos()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Monitor.toSafeNanos()
方法的具体详情如下:
包路径:com.google.common.util.concurrent.Monitor
类名称:Monitor
方法名:toSafeNanos
[英]Returns unit.toNanos(time), additionally ensuring the returned value is not at risk of overflowing or underflowing, by bounding the value between 0 and (Long.MAX_VALUE / 4) * 3. Actually waiting for more than 219 years is not supported!
[中]返回单位。toNanos(时间),通过将值限制在0和(Long.MAX_值/4)*3之间,进一步确保返回值不存在溢出或下溢的风险。实际上,等待超过219年是不受支持的!
代码示例来源:origin: google/guava
/**
* Enters this monitor. Blocks at most the given time.
*
* @return whether the monitor was entered
*/
@SuppressWarnings("GoodTime") // should accept a java.time.Duration
public boolean enter(long time, TimeUnit unit) {
final long timeoutNanos = toSafeNanos(time, unit);
final ReentrantLock lock = this.lock;
if (!fair && lock.tryLock()) {
return true;
}
boolean interrupted = Thread.interrupted();
try {
final long startTime = System.nanoTime();
for (long remainingNanos = timeoutNanos; ; ) {
try {
return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException interrupt) {
interrupted = true;
remainingNanos = remainingNanos(startTime, timeoutNanos);
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
代码示例来源:origin: google/guava
/**
* Waits for the guard to be satisfied. Waits at most the given time, and may be interrupted. May
* be called only by a thread currently occupying this monitor.
*
* @return whether the guard is now satisfied
* @throws InterruptedException if interrupted while waiting
*/
@SuppressWarnings("GoodTime") // should accept a java.time.Duration
public boolean waitFor(Guard guard, long time, TimeUnit unit) throws InterruptedException {
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
}
if (guard.isSatisfied()) {
return true;
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
return awaitNanos(guard, timeoutNanos, true);
}
代码示例来源:origin: wildfly/wildfly
/**
* Enters this monitor. Blocks at most the given time.
*
* @return whether the monitor was entered
*/
public boolean enter(long time, TimeUnit unit) {
final long timeoutNanos = toSafeNanos(time, unit);
final ReentrantLock lock = this.lock;
if (!fair && lock.tryLock()) {
return true;
}
boolean interrupted = Thread.interrupted();
try {
final long startTime = System.nanoTime();
for (long remainingNanos = timeoutNanos; ; ) {
try {
return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException interrupt) {
interrupted = true;
remainingNanos = remainingNanos(startTime, timeoutNanos);
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
代码示例来源:origin: google/guava
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
代码示例来源:origin: wildfly/wildfly
/**
* Waits for the guard to be satisfied. Waits at most the given time, and may be interrupted. May
* be called only by a thread currently occupying this monitor.
*
* @return whether the guard is now satisfied
* @throws InterruptedException if interrupted while waiting
*/
public boolean waitFor(Guard guard, long time, TimeUnit unit) throws InterruptedException {
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
}
if (guard.isSatisfied()) {
return true;
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
return awaitNanos(guard, timeoutNanos, true);
}
代码示例来源:origin: google/guava
final long timeoutNanos = toSafeNanos(time, unit);
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
代码示例来源:origin: google/guava
final long timeoutNanos = toSafeNanos(time, unit);
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
代码示例来源:origin: wildfly/wildfly
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
代码示例来源:origin: wildfly/wildfly
final long timeoutNanos = toSafeNanos(time, unit);
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
代码示例来源:origin: wildfly/wildfly
final long timeoutNanos = toSafeNanos(time, unit);
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
代码示例来源:origin: com.diffplug.guava/guava-concurrent
/**
* Enters this monitor. Blocks at most the given time.
*
* @return whether the monitor was entered
*/
public boolean enter(long time, TimeUnit unit) {
final long timeoutNanos = toSafeNanos(time, unit);
final ReentrantLock lock = this.lock;
if (!fair && lock.tryLock()) {
return true;
}
boolean interrupted = Thread.interrupted();
try {
final long startTime = System.nanoTime();
for (long remainingNanos = timeoutNanos;;) {
try {
return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException interrupt) {
interrupted = true;
remainingNanos = remainingNanos(startTime, timeoutNanos);
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
代码示例来源:origin: org.kill-bill.billing/killbill-platform-osgi-bundles-logger
/**
* Waits for the guard to be satisfied. Waits at most the given time, and may be interrupted. May
* be called only by a thread currently occupying this monitor.
*
* @return whether the guard is now satisfied
* @throws InterruptedException if interrupted while waiting
*/
public boolean waitFor(Guard guard, long time, TimeUnit unit) throws InterruptedException {
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
}
if (guard.isSatisfied()) {
return true;
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
return awaitNanos(guard, timeoutNanos, true);
}
代码示例来源:origin: org.jboss.eap/wildfly-client-all
/**
* Enters this monitor. Blocks at most the given time.
*
* @return whether the monitor was entered
*/
public boolean enter(long time, TimeUnit unit) {
final long timeoutNanos = toSafeNanos(time, unit);
final ReentrantLock lock = this.lock;
if (!fair && lock.tryLock()) {
return true;
}
boolean interrupted = Thread.interrupted();
try {
final long startTime = System.nanoTime();
for (long remainingNanos = timeoutNanos; ; ) {
try {
return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException interrupt) {
interrupted = true;
remainingNanos = remainingNanos(startTime, timeoutNanos);
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
代码示例来源:origin: org.kill-bill.billing/killbill-platform-osgi-bundles-logger
/**
* Enters this monitor. Blocks at most the given time.
*
* @return whether the monitor was entered
*/
public boolean enter(long time, TimeUnit unit) {
final long timeoutNanos = toSafeNanos(time, unit);
final ReentrantLock lock = this.lock;
if (!fair && lock.tryLock()) {
return true;
}
boolean interrupted = Thread.interrupted();
try {
final long startTime = System.nanoTime();
for (long remainingNanos = timeoutNanos; ; ) {
try {
return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException interrupt) {
interrupted = true;
remainingNanos = remainingNanos(startTime, timeoutNanos);
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
代码示例来源:origin: com.diffplug.guava/guava-concurrent
/**
* Waits for the guard to be satisfied. Waits at most the given time, and may be interrupted.
* May be called only by a thread currently occupying this monitor.
*
* @return whether the guard is now satisfied
* @throws InterruptedException if interrupted while waiting
*/
public boolean waitFor(Guard guard, long time, TimeUnit unit) throws InterruptedException {
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
}
if (guard.isSatisfied()) {
return true;
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
return awaitNanos(guard, timeoutNanos, true);
}
代码示例来源:origin: org.jboss.eap/wildfly-client-all
/**
* Waits for the guard to be satisfied. Waits at most the given time, and may be interrupted. May
* be called only by a thread currently occupying this monitor.
*
* @return whether the guard is now satisfied
* @throws InterruptedException if interrupted while waiting
*/
public boolean waitFor(Guard guard, long time, TimeUnit unit) throws InterruptedException {
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
}
if (guard.isSatisfied()) {
return true;
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
return awaitNanos(guard, timeoutNanos, true);
}
代码示例来源:origin: com.diffplug.guava/guava-concurrent
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
代码示例来源:origin: org.kill-bill.billing/killbill-platform-osgi-bundles-logger
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
代码示例来源:origin: org.jboss.eap/wildfly-client-all
final long timeoutNanos = toSafeNanos(time, unit);
if (!((guard.monitor == this) & lock.isHeldByCurrentThread())) {
throw new IllegalMonitorStateException();
代码示例来源:origin: com.diffplug.guava/guava-concurrent
final long timeoutNanos = toSafeNanos(time, unit);
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
内容来源于网络,如有侵权,请联系作者删除!