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


Java Semaphore.acquireUninterruptibly方法代码示例

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


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

示例1: loop

import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
final int loop(int n) {
    final Semaphore sem = this.sem;
    int sum = 0;
    int x = 0;
    while (n-- > 0) {
        sem.acquireUninterruptibly();
        try {
            x = setValue(LoopHelpers.compute1(getValue()));
        }
        finally {
            sem.release();
        }
        sum += LoopHelpers.compute2(x);
    }
    return sum;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:CheckedLockLoops.java

示例2: lock

import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
/**
 * 锁定key,其他等待此key的线程将进入等待,直到调用{@link #unlock(K)}
 * 使用hashcode和equals来判断key是否相同,因此key必须实现{@link #hashCode()}和
 * {@link #equals(Object)}方法
 *
 * @param key
 */
public void lock(K key) {
    if (key == null)
        return;
    LockInfo info = local.get().get(key);
    if (info == null) {
        Semaphore current = new Semaphore(1);
        current.acquireUninterruptibly();
        Semaphore previous = map.put(key, current);
        if (previous != null)
            previous.acquireUninterruptibly();
        local.get().put(key, new LockInfo(current));
    } else {
        info.lockCount++;
    }
}
 
开发者ID:gzxishan,项目名称:OftenPorter,代码行数:23,代码来源:ConcurrentKeyLock.java

示例3: getResult

import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
public R getResult() {
  final List<R> results = new ArrayList<>(1);
  final Semaphore semaphore = new Semaphore(0);
  run(new Subscriber<R>() {
    @Override public void onSuccess(R result) {
      results.add(0, result);
      semaphore.release();
    }

    @Override public void onError(Exception exception) {
      semaphore.release();
    }
  });
  semaphore.acquireUninterruptibly();
  return !results.isEmpty() ? results.get(0) : null;
}
 
开发者ID:softonic,项目名称:instamaterial,代码行数:17,代码来源:ObservableTask.java

示例4: save

import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
public Publisher<Integer> save(Publisher<Customer> customers) throws IOException {
  AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
  AtomicLong offset = new AtomicLong(0);
  AtomicInteger resultCount = new AtomicInteger(0);
  SingleItemPublisher<Integer> resultPublisher = new SingleItemPublisher<>();
  Semaphore writeSemaphore = new Semaphore(1);
  writeSemaphore.acquireUninterruptibly();
  fileChannel.write(ByteBuffer.wrap("[".getBytes()), 0, resultPublisher,
      andThen((count, s) -> {
        writeSemaphore.release();
        customers.subscribe(pullEach((Customer customer, Subscription subscription) -> {
            String json = String.format("%s{\"firstName\": \"%s\", \"lastName\": \"%s\"}", offset.longValue() == 0 ? "" : ",",
                customer.getFirstName(), customer.getLastName());
            offset.addAndGet(count);
            writeSemaphore.acquireUninterruptibly();
            fileChannel.write(ByteBuffer.wrap(json.getBytes()), offset.get(), resultPublisher,
                andThen((size, c) -> {
                  writeSemaphore.release();
                  offset.addAndGet(size);
                  resultCount.incrementAndGet();
                  subscription.request(1);
                }));
          }).andThen(() -> {
            writeSemaphore.acquireUninterruptibly();
              fileChannel.write(ByteBuffer.wrap("]".getBytes()), offset.longValue(), resultPublisher,
                  andThen((d, e) -> {
                    writeSemaphore.release();
                    try {
                      fileChannel.close();
                      resultPublisher.publish(resultCount.intValue());
                    } catch (IOException error) {
                      resultPublisher.publish(error);
                    }
                  }));
          }).exceptionally(error -> resultPublisher.publish(error)));
      }));
  return resultPublisher;
}
 
开发者ID:openknowledge,项目名称:reactive-jax-rs,代码行数:39,代码来源:CustomerRepository.java

示例5: startTest

import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
private void startTest() {
    stopped.set(false);
    RateLimiter limiter = RateLimiter.create(rate);
    Semaphore s = new Semaphore(100);
    while (!stopped.get()) {
        limiter.acquire();
        s.acquireUninterruptibly();
        counters.get(RandomUtils.nextInt(TOTAL_COUNTERS)).incrementAndGet().whenComplete((r, e) -> {
            s.release();
            if (e == null) {
                increments.incrementAndGet();
            }
        });
    }
}
 
开发者ID:shlee89,项目名称:athena,代码行数:16,代码来源:DistributedConsensusLoadTest.java

示例6: SyncCallback

import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
public SyncCallback() {
    m_response = null;
    m_lock = new Semaphore(1);
    m_lock.acquireUninterruptibly();
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:6,代码来源:SyncCallback.java

示例7: acquire

import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
void acquire(Semaphore s) {
    s.acquireUninterruptibly();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:4,代码来源:SemaphoreTest.java


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