本文整理汇总了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;
}
示例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++;
}
}
示例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;
}
示例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;
}
示例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();
}
});
}
}
示例6: SyncCallback
import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
public SyncCallback() {
m_response = null;
m_lock = new Semaphore(1);
m_lock.acquireUninterruptibly();
}
示例7: acquire
import java.util.concurrent.Semaphore; //导入方法依赖的package包/类
void acquire(Semaphore s) {
s.acquireUninterruptibly();
}