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


Java ReentrantLock.tryLock方法代码示例

本文整理汇总了Java中java.util.concurrent.locks.ReentrantLock.tryLock方法的典型用法代码示例。如果您正苦于以下问题:Java ReentrantLock.tryLock方法的具体用法?Java ReentrantLock.tryLock怎么用?Java ReentrantLock.tryLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.locks.ReentrantLock的用法示例。


在下文中一共展示了ReentrantLock.tryLock方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: tReentrantLock

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
public void tReentrantLock() {
    System.currentTimeMillis();
    ReentrantLock lock = new ReentrantLock();

    long t1 = System.currentTimeMillis();
    for (int i = 0; i < 10000000; i++) {
        if (lock.tryLock()) {
            try {
                // ...
            } finally {
                lock.unlock();
            }
        }
    }
    long t2 = System.currentTimeMillis();

    System.out.println("take time:" + (t2 - t1) + " ms.");
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:19,代码来源:LockPerfMain.java

示例2: enter

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
 * 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();
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:29,代码来源:Monitor.java

示例3: enterIfInterruptibly

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
 * Enters this monitor if the guard is satisfied. Blocks at most the given time acquiring the
 * lock, but does not wait for the guard to be satisfied, and may be interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 */
public boolean enterIfInterruptibly(Guard guard, long time, TimeUnit unit)
    throws InterruptedException {
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  if (!lock.tryLock(time, unit)) {
    return false;
  }

  boolean satisfied = false;
  try {
    return satisfied = guard.isSatisfied();
  } finally {
    if (!satisfied) {
      lock.unlock();
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:26,代码来源:Monitor.java

示例4: tryEnterIf

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
 * Enters this monitor if it is possible to do so immediately and the guard is satisfied. Does not
 * block acquiring the lock and does not wait for the guard to be satisfied.
 *
 * <p><b>Note:</b> This method disregards the fairness setting of this monitor.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 */
public boolean tryEnterIf(Guard guard) {
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  if (!lock.tryLock()) {
    return false;
  }

  boolean satisfied = false;
  try {
    return satisfied = guard.isSatisfied();
  } finally {
    if (!satisfied) {
      lock.unlock();
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:27,代码来源:Monitor.java

示例5: run

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
public final void run() {
    try {
        barrier.await();
        int sum = v;
        int x = 17;
        final ReentrantLock lock = this.lock;
        while (lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
            try {
                v = x = LoopHelpers.compute1(v);
            }
            finally {
                lock.unlock();
            }
            sum += LoopHelpers.compute2(x);
        }
        barrier.await();
        result += sum;
    }
    catch (Throwable ex) {
        fail = ex;
        throw new RuntimeException(ex);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:TimeoutLockLoops.java

示例6: helpExpungeStaleExceptions

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
 * If lock is available, poll stale refs and remove them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
开发者ID:monix,项目名称:monix-forkjoin,代码行数:15,代码来源:ForkJoinTask.java

示例7: run

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
public void run() {
  while (true) {
    for (ConcurrentHashMap.Entry<String, BlockingQueue<CopyJob>> copyEntry : queues.entrySet()) {
      try {
        String currentTableName = copyEntry.getKey();
        BlockingQueue<CopyJob> queue = copyEntry.getValue();

        ReentrantLock currentTableLock = tableCopyLocks.get(currentTableName);
        if (!currentTableLock.tryLock()) {
          continue;
        }

        try {
          // wait a second to take an item
          CopyJob firstJob = queue.poll(1L, TimeUnit.SECONDS);
          // when there is no item, go to the next entry in the loop
          if (firstJob == null) continue;

          Config.Table table = firstJob.getTable();
          ArrayList<CopyJob> jobs = new ArrayList<>();
          jobs.add(firstJob);
          // drain up to 9 more for 10 in total
          queue.drainTo(jobs, 9);

          processJobs(table, jobs);
        } finally {
          currentTableLock.unlock();
        }
      } catch (InterruptedException ie) {
        return; // do nothing and return
      } catch (Exception e) {
        App.fatal(e);
      }
    }
  }
}
 
开发者ID:Patreon,项目名称:euphrates,代码行数:37,代码来源:S3Writer.java

示例8: helpExpungeStaleExceptions

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
 * If lock is available, polls stale refs and removes them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:ForkJoinTask.java

示例9: helpExpungeStaleExceptions

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
 * If lock is available, poll stale refs and remove them. Called from
 * ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
	final ReentrantLock lock = exceptionTableLock;
	if (lock.tryLock()) {
		try {
			expungeStaleExceptions();
		} finally {
			lock.unlock();
		}
	}
}
 
开发者ID:zhangjunfang,项目名称:util,代码行数:15,代码来源:ForkJoinTask.java

示例10: enterWhen

import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
 * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both
 * the time to acquire the lock and the time to wait for the guard to be satisfied, and may be
 * interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 * @throws InterruptedException if interrupted while waiting
 */
public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException {
  final long timeoutNanos = toSafeNanos(time, unit);
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  boolean reentrant = lock.isHeldByCurrentThread();
  long startTime = 0L;

  locked:
  {
    if (!fair) {
      // Check interrupt status to get behavior consistent with fair case.
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      if (lock.tryLock()) {
        break locked;
      }
    }
    startTime = initNanoTime(timeoutNanos);
    if (!lock.tryLock(time, unit)) {
      return false;
    }
  }

  boolean satisfied = false;
  boolean threw = true;
  try {
    satisfied =
        guard.isSatisfied()
            || awaitNanos(
                guard,
                (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos),
                reentrant);
    threw = false;
    return satisfied;
  } finally {
    if (!satisfied) {
      try {
        // Don't need to signal if timed out, but do if interrupted
        if (threw && !reentrant) {
          signalNextWaiter();
        }
      } finally {
        lock.unlock();
      }
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:59,代码来源:Monitor.java


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