本文整理了Java中java.util.concurrent.Semaphore.acquire()
方法的一些代码示例,展示了Semaphore.acquire()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Semaphore.acquire()
方法的具体详情如下:
包路径:java.util.concurrent.Semaphore
类名称:Semaphore
方法名:acquire
[英]Acquires a permit from this semaphore, blocking until one is available, or the thread is Thread#interrupt.
Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.
If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
If the current thread:
代码示例来源:origin: redisson/redisson
public void waitForFinish() throws InterruptedException {
final Semaphore sem = new Semaphore(0);
orderedPool.execute(new Runnable() {
@Override
public void run() {
sem.release();
}
});
sem.acquire();
}
代码示例来源:origin: redisson/redisson
public void addCall(final FSTRunnable toRun) throws InterruptedException {
gateway.acquire();
if (jobs[curIdx] == null) {
jobs[curIdx] = toRun;
} else {
jobs[curIdx].sem.acquire();
jobs[curIdx].sem.release();
jobs[curIdx] = toRun;
}
toRun.sem = sems[curIdx];
toRun.sem.acquire();
OrderedRunnable ord = orderedRunnableCache[curIdx];
ord.toRun = toRun;
curIdx = (curIdx + 1) % threads;
orderedPool.execute(ord);
pool.execute(toRun);
}
代码示例来源:origin: stackoverflow.com
public class BoundedExecutor {
private final Executor exec;
private final Semaphore semaphore;
public BoundedExecutor(Executor exec, int bound) {
this.exec = exec;
this.semaphore = new Semaphore(bound);
}
public void submitTask(final Runnable command)
throws InterruptedException, RejectedExecutionException {
semaphore.acquire();
try {
exec.execute(new Runnable() {
public void run() {
try {
command.run();
} finally {
semaphore.release();
}
}
});
} catch (RejectedExecutionException e) {
semaphore.release();
throw e;
}
}
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testRunningJobIsInterruptedAfterShutdownNow() throws InterruptedException {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
ExecutorService service = Executors.newSingleThreadExecutor();
try {
PartitionedOrderedExecutor executor = new PartitionedOrderedExecutor(queue, service);
final Semaphore jobSemaphore = new Semaphore(0);
final Semaphore testSemaphore = new Semaphore(0);
final AtomicBoolean interrupted = new AtomicBoolean();
executor.submit(() -> {
testSemaphore.release();
try {
jobSemaphore.acquire();
} catch (InterruptedException e) {
interrupted.set(true);
}
});
testSemaphore.acquireUninterruptibly();
assertThat(executor.shutdownNow(), empty());
assertThat(executor.awaitTermination(2, MINUTES), is(true));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(true));
assertThat(jobSemaphore.availablePermits(), is(0));
assertThat(interrupted.get(), is(true));
} finally {
service.shutdown();
}
}
代码示例来源:origin: stackoverflow.com
private static Semaphore semaphore = new Semaphore(1024 * 1024, true);
semaphore.acquire(size);
semaphore.release(array.length);
代码示例来源:origin: apache/geode
@Override
public void lockInterruptibly() throws InterruptedException {
readerSemaphore.acquire();
try {
numReaders++;
if (numReaders == 1) {
writerSemaphore.acquire();
}
} finally {
// in case writeSemaphore.acquire throws Exception
readerSemaphore.release();
}
}
代码示例来源:origin: RuedigerMoeller/fast-serialization
public void waitForFinish() throws InterruptedException {
final Semaphore sem = new Semaphore(0);
orderedPool.execute(new Runnable() {
@Override
public void run() {
sem.release();
}
});
sem.acquire();
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testRunningJobIsInterruptedAfterShutdownNow() throws InterruptedException {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
ExecutorService service = Executors.newSingleThreadExecutor();
try {
PartitionedUnorderedExecutor executor = new PartitionedUnorderedExecutor(queue, service, 1);
final Semaphore jobSemaphore = new Semaphore(0);
final Semaphore testSemaphore = new Semaphore(0);
final AtomicBoolean interrupted = new AtomicBoolean();
executor.submit(() -> {
testSemaphore.release();
try {
jobSemaphore.acquire();
} catch (InterruptedException e) {
interrupted.set(true);
}
});
testSemaphore.acquireUninterruptibly();
assertThat(executor.shutdownNow(), empty());
assertThat(executor.awaitTermination(2, MINUTES), is(true));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(true));
assertThat(jobSemaphore.availablePermits(), is(0));
assertThat(interrupted.get(), is(true));
} finally {
service.shutdown();
}
}
代码示例来源:origin: jenkinsci/jenkins
mutexByTool.put(tool, semaphore = new Semaphore(1));
semaphore.acquire();
try {
return installer.performInstallation(tool, node, log).getRemote();
} finally {
semaphore.release();
代码示例来源:origin: redisson/redisson
@Override
public void run() {
try {
toRun.sem.acquire();
toRun.runInOrder();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
toRun.sem.release();
gateway.release();
}
}
}
代码示例来源:origin: apache/storm
public void feed(Object tuples) {
Semaphore sem = new Semaphore(0);
((List) RegisteredGlobalState.getState(_semaphoreId)).add(sem);
((List) RegisteredGlobalState.getState(_id)).add(tuples);
try {
sem.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testRunningJobIsInterruptedAfterShutdownNow() throws InterruptedException {
ExecutorService worker = Executors.newSingleThreadExecutor();
try {
PartitionedScheduledExecutor executor = new PartitionedScheduledExecutor(scheduler, worker);
final Semaphore jobSemaphore = new Semaphore(0);
final Semaphore testSemaphore = new Semaphore(0);
final AtomicBoolean interrupted = new AtomicBoolean();
executor.submit(() -> {
testSemaphore.release();
try {
jobSemaphore.acquire();
} catch (InterruptedException e) {
interrupted.set(true);
}
});
testSemaphore.acquireUninterruptibly();
assertThat(executor.shutdownNow(), empty());
assertThat(executor.awaitTermination(2, MINUTES), is(true));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(true));
assertThat(jobSemaphore.availablePermits(), is(0));
assertThat(interrupted.get(), is(true));
} finally {
worker.shutdown();
}
}
代码示例来源:origin: Netflix/concurrency-limits
public void run(int iterations, int limit, Executor executor, Supplier<Long> latency) {
AtomicInteger requests = new AtomicInteger();
AtomicInteger busy = new AtomicInteger();
AtomicInteger counter = new AtomicInteger();
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
System.out.println("" + counter.incrementAndGet() + " total=" + requests.getAndSet(0) + " busy=" + busy.get());
}, 1, 1, TimeUnit.SECONDS);
Semaphore sem = new Semaphore(limit, true);
for (int i = 0; i < iterations; i++) {
requests.incrementAndGet();
busy.incrementAndGet();
executor.execute(() -> {
try {
sem.acquire();
TimeUnit.MILLISECONDS.sleep(latency.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
sem.release();
busy.decrementAndGet();
}
});
}
}
代码示例来源:origin: LeonardoZ/java-concurrency-patterns
public T get(long secondsToTimeout) throws InterruptedException {
semaphore.acquire();
try {
T resource = resources.poll(secondsToTimeout, TIME_UNIT);
return resource;
} finally {
semaphore.release();
}
}
代码示例来源:origin: alibaba/jstorm
public void feed(Object tuples) {
Semaphore sem = new Semaphore(0);
((List) RegisteredGlobalState.getState(_semaphoreId)).add(sem);
((List) RegisteredGlobalState.getState(_id)).add(tuples);
try {
sem.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testRunningJobsAreInterruptedAfterShutdownNow() throws InterruptedException {
final int jobCount = 4;
ExecutorService worker = Executors.newCachedThreadPool();
try {
PartitionedScheduledExecutor executor = new PartitionedScheduledExecutor(scheduler, worker);
final Semaphore jobSemaphore = new Semaphore(0);
final Semaphore testSemaphore = new Semaphore(0);
final AtomicInteger interrupted = new AtomicInteger();
for (int i = 0; i < jobCount; i++) {
executor.submit(() -> {
testSemaphore.release();
try {
jobSemaphore.acquire();
} catch (InterruptedException e) {
interrupted.incrementAndGet();
}
});
}
testSemaphore.acquireUninterruptibly(jobCount);
assertThat(executor.shutdownNow(), empty());
assertThat(executor.awaitTermination(2, MINUTES), is(true));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(true));
assertThat(jobSemaphore.availablePermits(), is(0));
assertThat(interrupted.get(), is(jobCount));
} finally {
worker.shutdown();
}
}
代码示例来源:origin: spullara/mustache.java
private AtomicInteger render(Mustache test, ExecutorService es) throws InterruptedException {
final AtomicInteger total = new AtomicInteger();
final Semaphore semaphore = new Semaphore(100);
for (int i = 0; i < 100000; i++) {
semaphore.acquire();
es.submit(() -> {
try {
TestObject testObject = new TestObject(r.nextInt(), r.nextInt(), r.nextInt());
StringWriter sw = new StringWriter();
test.execute(sw, testObject).close();
if (!render(testObject).equals(sw.toString())) {
total.incrementAndGet();
}
} catch (IOException e) {
// Can't fail
e.printStackTrace();
System.exit(1);
} finally {
semaphore.release();
}
});
}
// Wait for them all to complete
semaphore.acquire(100);
return total;
}
代码示例来源:origin: RuedigerMoeller/fast-serialization
public void addCall(final FSTRunnable toRun) throws InterruptedException {
gateway.acquire();
if (jobs[curIdx] == null) {
jobs[curIdx] = toRun;
} else {
jobs[curIdx].sem.acquire();
jobs[curIdx].sem.release();
jobs[curIdx] = toRun;
}
toRun.sem = sems[curIdx];
toRun.sem.acquire();
OrderedRunnable ord = orderedRunnableCache[curIdx];
ord.toRun = toRun;
curIdx = (curIdx + 1) % threads;
orderedPool.execute(ord);
pool.execute(toRun);
}
代码示例来源:origin: cats-oss/android-gpuimage
/**
* Capture the current image with the size as it is displayed and retrieve it as Bitmap.
*
* @return current output as Bitmap
* @throws InterruptedException
*/
public Bitmap capture() throws InterruptedException {
final Semaphore waiter = new Semaphore(0);
final int width = surfaceView.getMeasuredWidth();
final int height = surfaceView.getMeasuredHeight();
// Take picture on OpenGL thread
final Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
gpuImage.runOnGLThread(new Runnable() {
@Override
public void run() {
GPUImageNativeLibrary.adjustBitmap(resultBitmap);
waiter.release();
}
});
requestRender();
waiter.acquire();
return resultBitmap;
}
代码示例来源:origin: ehcache/ehcache3
PartitionedUnorderedExecutor executor = new PartitionedUnorderedExecutor(queue, service, jobCount);
final Semaphore jobSemaphore = new Semaphore(0);
final Semaphore testSemaphore = new Semaphore(0);
final AtomicInteger interrupted = new AtomicInteger();
testSemaphore.release();
try {
jobSemaphore.acquire();
} catch (InterruptedException e) {
interrupted.incrementAndGet();
内容来源于网络,如有侵权,请联系作者删除!