本文整理了Java中java.util.concurrent.Semaphore.<init>()
方法的一些代码示例,展示了Semaphore.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Semaphore.<init>()
方法的具体详情如下:
包路径:java.util.concurrent.Semaphore
类名称:Semaphore
方法名:<init>
[英]Creates a Semaphore with the given number of permits and nonfair fairness setting.
[中]创建具有给定许可数和非空中公平设置的信号量。
代码示例来源: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: Red5/red5-server
/** {@inheritDoc} */
@Override
public boolean start(IScope scope) {
if (lock == null) {
lock = new Semaphore(1, true);
}
try {
lock.tryAcquire(1, TimeUnit.SECONDS);
return super.start(scope);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.release();
}
return false;
}
代码示例来源: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: ehcache/ehcache3
@Test
public void testFaultsDoNotGetToEvictionAdvisor() throws StoreAccessException {
final Semaphore semaphore = new Semaphore(0);
final OnHeapStoreForTests<String, String> store = newStore(SystemTimeSource.INSTANCE, noAdvice());
ExecutorService executor = Executors.newCachedThreadPool();
try {
executor.submit(() -> store.getOrComputeIfAbsent("prime", key -> {
semaphore.acquireUninterruptibly();
return new OnHeapValueHolder<String>(0, 0, false) {
@Override
public String get() {
return key;
}
};
}));
while (!semaphore.hasQueuedThreads());
store.put("boom", "boom");
} finally {
semaphore.release(1);
executor.shutdown();
}
}
代码示例来源:origin: stackoverflow.com
private static Semaphore semaphore = new Semaphore(1024 * 1024, true);
semaphore.acquire(size);
semaphore.release(array.length);
代码示例来源:origin: LeonardoZ/java-concurrency-patterns
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
Semaphore semaphore = new Semaphore(3);
Runnable r = () -> {
try {
System.out.println("Trying to acquire - " + Thread.currentThread().getName());
if (semaphore.tryAcquire(2, TimeUnit.SECONDS)) {
// use-get resource
// simulate work in progress
System.out.println("Acquired - " + Thread.currentThread().getName());
Thread.sleep(2000);
System.out.println("Done - " + Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
};
for (int i = 0; i < 4; i++) {
executor.execute(r);
}
executor.shutdown();
}
}
代码示例来源: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: ehcache/ehcache3
@Test
public void testQueuedJobIsStoppedAfterShutdownNow() 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);
executor.submit(() -> {
testSemaphore.release();
jobSemaphore.acquireUninterruptibly();
});
final AtomicBoolean called = new AtomicBoolean();
executor.submit(() -> called.set(true));
testSemaphore.acquireUninterruptibly();
assertThat(executor.shutdownNow(), hasSize(1));
assertThat(executor.awaitTermination(100, MILLISECONDS), is(false));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(false));
jobSemaphore.release();
assertThat(executor.awaitTermination(2, MINUTES), is(true));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(true));
assertThat(jobSemaphore.availablePermits(), is(0));
assertThat(called.get(), is(false));
} 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: apache/flume
memQueRemaining = new Semaphore(newMemoryCapacity);
} else {
int diff = newMemoryCapacity - memoryCapacity;
memQueRemaining.release(diff);
代码示例来源: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: ehcache/ehcache3
@Test
public void testQueuedJobIsStoppedAfterShutdownNow() 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);
executor.submit(() -> {
testSemaphore.release();
jobSemaphore.acquireUninterruptibly();
});
final AtomicBoolean called = new AtomicBoolean();
executor.submit(() -> called.set(true));
testSemaphore.acquireUninterruptibly();
assertThat(executor.shutdownNow(), hasSize(1));
assertThat(executor.awaitTermination(100, MILLISECONDS), is(false));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(false));
jobSemaphore.release();
assertThat(executor.awaitTermination(2, MINUTES), is(true));
assertThat(executor.isShutdown(), is(true));
assertThat(executor.isTerminated(), is(true));
assertThat(jobSemaphore.availablePermits(), is(0));
assertThat(called.get(), is(false));
} 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: stackoverflow.com
private Semaphore terminations = new Semaphore(0);
protected void beforeExecute (final Thread thread, final Runnable job) {
if (terminations.tryAcquire()) {
/* Replace this item in the queue so it may be executed by another
* thread
*/
queue.add(job);
thread.setUncaughtExceptionHandler(
new ShutdownHandler(thread.getUncaughtExceptionHandler())
);
/* Throwing a runtime exception is the only way to prematurely
* cause a worker thread from the TheadPoolExecutor to exit.
*/
throw new ShutdownException("Terminating thread");
}
}
public void setCorePoolSize (final int size) {
int delta = getActiveCount() - size;
super.setCorePoolSize(size);
if (delta > 0) {
terminations.release(delta);
}
}
代码示例来源: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
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();
代码示例来源:origin: ehcache/ehcache3
PartitionedOrderedExecutor executor = new PartitionedOrderedExecutor(queue, service);
final Semaphore jobSemaphore = new Semaphore(0);
final Semaphore testSemaphore = new Semaphore(0);
testSemaphore.release();
jobSemaphore.acquireUninterruptibly();
});
assertThat(executor.isTerminated(), is(false));
jobSemaphore.release();
assertThat(executor.awaitTermination(2, MINUTES), is(true));
assertThat(executor.isShutdown(), is(true));
内容来源于网络,如有侵权,请联系作者删除!