com.google.common.util.concurrent.Monitor.enter()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中com.google.common.util.concurrent.Monitor.enter()方法的一些代码示例,展示了Monitor.enter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Monitor.enter()方法的具体详情如下:
包路径:com.google.common.util.concurrent.Monitor
类名称:Monitor
方法名:enter

Monitor.enter介绍

[英]Enters this monitor. Blocks indefinitely.
[中]进入这个监视器。无限期地阻塞。

代码示例

代码示例来源:origin: google/guava

/**
 * Attempts to start the timer immediately prior to the service being started via {@link
 * Service#startAsync()}.
 */
void tryStartTiming(Service service) {
 monitor.enter();
 try {
  Stopwatch stopwatch = startupTimers.get(service);
  if (stopwatch == null) {
   startupTimers.put(service, Stopwatch.createStarted());
  }
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/guava

void awaitStopped(long timeout, TimeUnit unit) throws TimeoutException {
 monitor.enter();
 try {
  if (!monitor.waitForUninterruptibly(stoppedGuard, timeout, unit)) {
   throw new TimeoutException(
     "Timeout waiting for the services to stop. The following "
       + "services have not stopped: "
       + Multimaps.filterKeys(servicesByState, not(in(EnumSet.of(TERMINATED, FAILED)))));
  }
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/j2objc

/**
 * Attempts to start the timer immediately prior to the service being started via {@link
 * Service#startAsync()}.
 */
void tryStartTiming(Service service) {
 monitor.enter();
 try {
  Stopwatch stopwatch = startupTimers.get(service);
  if (stopwatch == null) {
   startupTimers.put(service, Stopwatch.createStarted());
  }
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

public void requestStarted() {
  int previousCount = activeRequests.getAndIncrement();
  totalRequests.incrementAndGet();
  if (previousCount == 0) {
    // previously there were no active requests, but now there are -- signal to any waitForQuiescence threads that they need to
    // begin waiting again
    monitor.enter();
    monitor.leave();
  }
}

代码示例来源:origin: google/guava

void awaitHealthy(long timeout, TimeUnit unit) throws TimeoutException {
 monitor.enter();
 try {
  if (!monitor.waitForUninterruptibly(awaitHealthGuard, timeout, unit)) {
   throw new TimeoutException(
     "Timeout waiting for the services to become healthy. The "
       + "following services have not started: "
       + Multimaps.filterKeys(servicesByState, in(ImmutableSet.of(NEW, STARTING))));
  }
  checkHealthy();
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

public void requestFinished() {
  int newCount = activeRequests.decrementAndGet();
  lastRequestFinishedNanos.set(System.nanoTime());
  if (newCount == 0) {
    // there are no active requests, so signal to any waitForQuiescence threads that they can begin waiting for their quietPeriod
    monitor.enter();
    monitor.leave();
  }
}

代码示例来源:origin: google/guava

ImmutableMap<Service, Long> startupTimes() {
 List<Entry<Service, Long>> loadTimes;
 monitor.enter();
 try {
  loadTimes = Lists.newArrayListWithCapacity(startupTimers.size());
  // N.B. There will only be an entry in the map if the service has started
  for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) {
   Service service = entry.getKey();
   Stopwatch stopWatch = entry.getValue();
   if (!stopWatch.isRunning() && !(service instanceof NoOpService)) {
    loadTimes.add(Maps.immutableEntry(service, stopWatch.elapsed(MILLISECONDS)));
   }
  }
 } finally {
  monitor.leave();
 }
 Collections.sort(
   loadTimes,
   Ordering.natural()
     .onResultOf(
       new Function<Entry<Service, Long>, Long>() {
        @Override
        public Long apply(Entry<Service, Long> input) {
         return input.getValue();
        }
       }));
 return ImmutableMap.copyOf(loadTimes);
}

代码示例来源:origin: wildfly/wildfly

/**
 * Attempts to start the timer immediately prior to the service being started via {@link
 * Service#startAsync()}.
 */
void tryStartTiming(Service service) {
 monitor.enter();
 try {
  Stopwatch stopwatch = startupTimers.get(service);
  if (stopwatch == null) {
   startupTimers.put(service, Stopwatch.createStarted());
  }
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/j2objc

void awaitStopped(long timeout, TimeUnit unit) throws TimeoutException {
 monitor.enter();
 try {
  if (!monitor.waitForUninterruptibly(stoppedGuard, timeout, unit)) {
   throw new TimeoutException(
     "Timeout waiting for the services to stop. The following "
       + "services have not stopped: "
       + Multimaps.filterKeys(servicesByState, not(in(EnumSet.of(TERMINATED, FAILED)))));
  }
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/guava

/**
 * Marks the {@link State} as ready to receive transitions. Returns true if no transitions have
 * been observed yet.
 */
void markReady() {
 monitor.enter();
 try {
  if (!transitioned) {
   // nothing has transitioned since construction, good.
   ready = true;
  } else {
   // This should be an extremely rare race condition.
   List<Service> servicesInBadStates = Lists.newArrayList();
   for (Service service : servicesByState().values()) {
    if (service.state() != NEW) {
     servicesInBadStates.add(service);
    }
   }
   throw new IllegalArgumentException(
     "Services started transitioning asynchronously before "
       + "the ServiceManager was constructed: "
       + servicesInBadStates);
  }
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/guava

ImmutableMultimap<State, Service> servicesByState() {
 ImmutableSetMultimap.Builder<State, Service> builder = ImmutableSetMultimap.builder();
 monitor.enter();
 try {
  for (Entry<State, Service> entry : servicesByState.entries()) {
   if (!(entry.getValue() instanceof NoOpService)) {
    builder.put(entry);
   }
  }
 } finally {
  monitor.leave();
 }
 return builder.build();
}

代码示例来源:origin: wildfly/wildfly

void awaitStopped(long timeout, TimeUnit unit) throws TimeoutException {
 monitor.enter();
 try {
  if (!monitor.waitForUninterruptibly(stoppedGuard, timeout, unit)) {
   throw new TimeoutException(
     "Timeout waiting for the services to stop. The following "
       + "services have not stopped: "
       + Multimaps.filterKeys(servicesByState, not(in(EnumSet.of(TERMINATED, FAILED)))));
  }
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/guava

private void enterSatisfyGuardAndLeaveInCurrentThread() {
 monitor.enter();
 try {
  guard.setSatisfied(true);
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/j2objc

void awaitHealthy(long timeout, TimeUnit unit) throws TimeoutException {
 monitor.enter();
 try {
  if (!monitor.waitForUninterruptibly(awaitHealthGuard, timeout, unit)) {
   throw new TimeoutException(
     "Timeout waiting for the services to become healthy. The "
       + "following services have not started: "
       + Multimaps.filterKeys(servicesByState, in(ImmutableSet.of(NEW, STARTING))));
  }
  checkHealthy();
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/j2objc

ImmutableMap<Service, Long> startupTimes() {
 List<Entry<Service, Long>> loadTimes;
 monitor.enter();
 try {
  loadTimes = Lists.newArrayListWithCapacity(startupTimers.size());
  // N.B. There will only be an entry in the map if the service has started
  for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) {
   Service service = entry.getKey();
   Stopwatch stopWatch = entry.getValue();
   if (!stopWatch.isRunning() && !(service instanceof NoOpService)) {
    loadTimes.add(Maps.immutableEntry(service, stopWatch.elapsed(MILLISECONDS)));
   }
  }
 } finally {
  monitor.leave();
 }
 Collections.sort(
   loadTimes,
   Ordering.natural()
     .onResultOf(
       new Function<Entry<Service, Long>, Long>() {
        @Override
        public Long apply(Entry<Service, Long> input) {
         return input.getValue();
        }
       }));
 return ImmutableMap.copyOf(loadTimes);
}

代码示例来源:origin: google/guava

/**
 * Invoke this method to transition the service to the {@link State#FAILED}. The service will
 * <b>not be stopped</b> if it is running. Invoke this method when a service has failed critically
 * or otherwise cannot be started nor stopped.
 */
protected final void notifyFailed(Throwable cause) {
 checkNotNull(cause);
 monitor.enter();
 try {
  State previous = state();
  switch (previous) {
   case NEW:
   case TERMINATED:
    throw new IllegalStateException("Failed while in state:" + previous, cause);
   case RUNNING:
   case STARTING:
   case STOPPING:
    snapshot = new StateSnapshot(FAILED, false, cause);
    enqueueFailedEvent(previous, cause);
    break;
   case FAILED:
    // Do nothing
    break;
  }
 } finally {
  monitor.leave();
  dispatchListenerEvents();
 }
}

代码示例来源:origin: wildfly/wildfly

void awaitHealthy(long timeout, TimeUnit unit) throws TimeoutException {
 monitor.enter();
 try {
  if (!monitor.waitForUninterruptibly(awaitHealthGuard, timeout, unit)) {
   throw new TimeoutException(
     "Timeout waiting for the services to become healthy. The "
       + "following services have not started: "
       + Multimaps.filterKeys(servicesByState, in(ImmutableSet.of(NEW, STARTING))));
  }
  checkHealthy();
 } finally {
  monitor.leave();
 }
}

代码示例来源:origin: google/guava

@Override
 public void run() {
  monitor.enter();
  try {
   enteredLatch.countDown();
   awaitUninterruptibly(tearDownLatch);
   guard.setSatisfied(true);
  } finally {
   monitor.leave();
  }
 }
});

代码示例来源:origin: google/guava

/**
 * Implementing classes should invoke this method once their service has stopped. It will cause
 * the service to transition from {@link State#STARTING} or {@link State#STOPPING} to {@link
 * State#TERMINATED}.
 *
 * @throws IllegalStateException if the service is not one of {@link State#STOPPING}, {@link
 *     State#STARTING}, or {@link State#RUNNING}.
 */
protected final void notifyStopped() {
 monitor.enter();
 try {
  State previous = state();
  switch (previous) {
   case NEW:
   case TERMINATED:
   case FAILED:
    throw new IllegalStateException("Cannot notifyStopped() when the service is " + previous);
   case RUNNING:
   case STARTING:
   case STOPPING:
    snapshot = new StateSnapshot(TERMINATED);
    enqueueTerminatedEvent(previous);
    break;
  }
 } finally {
  monitor.leave();
  dispatchListenerEvents();
 }
}

代码示例来源:origin: wildfly/wildfly

ImmutableMap<Service, Long> startupTimes() {
 List<Entry<Service, Long>> loadTimes;
 monitor.enter();
 try {
  loadTimes = Lists.newArrayListWithCapacity(startupTimers.size());
  // N.B. There will only be an entry in the map if the service has started
  for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) {
   Service service = entry.getKey();
   Stopwatch stopWatch = entry.getValue();
   if (!stopWatch.isRunning() && !(service instanceof NoOpService)) {
    loadTimes.add(Maps.immutableEntry(service, stopWatch.elapsed(MILLISECONDS)));
   }
  }
 } finally {
  monitor.leave();
 }
 Collections.sort(
   loadTimes,
   Ordering.natural()
     .onResultOf(
       new Function<Entry<Service, Long>, Long>() {
        @Override
        public Long apply(Entry<Service, Long> input) {
         return input.getValue();
        }
       }));
 return ImmutableMap.copyOf(loadTimes);
}

相关文章