use of java.util.concurrent.SynchronousQueue in project storm by apache.
the class SaslTransportPlugin method getServer.
@Override
public TServer getServer(TProcessor processor) throws IOException, TTransportException {
int configuredPort = type.getPort(conf);
Integer socketTimeout = type.getSocketTimeOut(conf);
TTransportFactory serverTransportFactory = getServerTransportFactory(type.isImpersonationAllowed());
TServerSocket serverTransport = null;
if (socketTimeout != null) {
serverTransport = new TServerSocket(configuredPort, socketTimeout);
} else {
serverTransport = new TServerSocket(configuredPort);
}
this.port = serverTransport.getServerSocket().getLocalPort();
int numWorkerThreads = type.getNumThreads(conf);
Integer queueSize = type.getQueueSize(conf);
TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport).processor(new TUGIWrapProcessor(processor)).minWorkerThreads(numWorkerThreads).maxWorkerThreads(numWorkerThreads).protocolFactory(new TBinaryProtocol.Factory(false, true));
if (serverTransportFactory != null) {
serverArgs.transportFactory(serverTransportFactory);
}
BlockingQueue<Runnable> workQueue = new SynchronousQueue<>();
if (queueSize != null) {
workQueue = new ArrayBlockingQueue<>(queueSize);
}
ThreadPoolExecutor executorService = new ExtendedThreadPoolExecutor(numWorkerThreads, numWorkerThreads, 60, TimeUnit.SECONDS, workQueue);
serverArgs.executorService(executorService);
return new TThreadPoolServer(serverArgs);
}
use of java.util.concurrent.SynchronousQueue in project hazelcast by hazelcast.
the class DelegateAndSkipOnConcurrentExecutionDecoratorTest method givenTheTaskIsAlreadyRunning_whenThreadAttemptToExecuteIt_theExutionWillBeSkipped.
@Test
public void givenTheTaskIsAlreadyRunning_whenThreadAttemptToExecuteIt_theExutionWillBeSkipped() throws InterruptedException {
final ResumableCountingRunnable task = new ResumableCountingRunnable();
final AtomicInteger counter = new AtomicInteger();
SynchronousQueue<Runnable> queue = new SynchronousQueue<Runnable>() {
@Override
public boolean offer(Runnable runnable) {
counter.incrementAndGet();
return super.offer(runnable);
}
};
ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, queue);
// start first task
DelegateAndSkipOnConcurrentExecutionDecorator decoratedTask = decorateAndInvokeRunOnDifferentThread(task, executor);
// wait until the task is running
task.awaitExecutionStarted();
// attempt to start execution from the test thread. this execution should be skipped -> it won't block
decoratedTask.run();
// resume the original task
task.resumeExecution();
assertEquals(1, task.getExecutionCount());
assertEquals(1, counter.get());
}
use of java.util.concurrent.SynchronousQueue in project deltaspike by apache.
the class ThreadPoolManager method find.
public ExecutorService find(final String name) {
if (closed) {
throw new IllegalStateException("Container is shutting down");
}
ExecutorService pool = pools.get(name);
if (pool == null) {
synchronized (this) {
pool = pools.get(name);
if (pool == null) {
// the instantiation does the following:
// 1. check if there is a named bean matching this name using @Default qualifier
// 2. check if there is a JNDI entry (ManagedExecutorService case) matching this name
// 3. create a new executor service based on the DS-config
// 1.
final Set<Bean<?>> beans = beanManager.getBeans(name);
if (beans != null && !beans.isEmpty()) {
final Bean<?> bean = beanManager.resolve(beans);
if (bean.getTypes().contains(ExecutorService.class)) {
final CreationalContext<Object> creationalContext = beanManager.createCreationalContext(null);
if (!beanManager.isNormalScope(bean.getScope())) {
contexts.add(creationalContext);
}
pool = ExecutorService.class.cast(beanManager.getReference(bean, ExecutorService.class, creationalContext));
}
}
if (// 2.
pool == null) {
for (final String prefix : asList("", "java:app/", "java:global/", "java:global/threads/", "java:global/deltaspike/", "java:")) {
try {
final Object instance = new InitialContext().lookup(prefix + name);
if (ExecutorService.class.isInstance(instance)) {
pool = ExecutorService.class.cast(instance);
break;
}
} catch (final NamingException e) {
// no-op
}
}
}
if (// 3.
pool == null) {
final String configPrefix = "futureable.pool." + name + ".";
final int coreSize = ConfigResolver.resolve(configPrefix + "coreSize").as(Integer.class).withDefault(Math.max(2, Runtime.getRuntime().availableProcessors())).getValue();
final int maxSize = ConfigResolver.resolve(configPrefix + "maxSize").as(Integer.class).withDefault(coreSize).getValue();
final long keepAlive = ConfigResolver.resolve(configPrefix + "keepAlive.value").as(Long.class).withDefault(0L).getValue();
final String keepAliveUnit = ConfigResolver.resolve(configPrefix + "keepAlive.unit").as(String.class).withDefault("MILLISECONDS").getValue();
final String queueType = ConfigResolver.resolve(configPrefix + "queue.type").as(String.class).withDefault("LINKED").getValue();
final BlockingQueue<Runnable> queue;
if ("ARRAY".equalsIgnoreCase(queueType)) {
final int size = ConfigResolver.resolve(configPrefix + "queue.size").as(Integer.class).withDefault(1024).getValue();
final boolean fair = ConfigResolver.resolve(configPrefix + "queue.fair").as(Boolean.class).withDefault(false).getValue();
queue = new ArrayBlockingQueue<Runnable>(size, fair);
} else if ("SYNCHRONOUS".equalsIgnoreCase(queueType)) {
final boolean fair = ConfigResolver.resolve(configPrefix + "queue.fair").as(Boolean.class).withDefault(false).getValue();
queue = new SynchronousQueue<Runnable>(fair);
} else {
final int capacity = ConfigResolver.resolve(configPrefix + "queue.capacity").as(Integer.class).withDefault(Integer.MAX_VALUE).getValue();
queue = new LinkedBlockingQueue<Runnable>(capacity);
}
final String threadFactoryName = ConfigResolver.getPropertyValue(configPrefix + "threadFactory.name");
final ThreadFactory threadFactory;
if (threadFactoryName != null) {
threadFactory = lookupByName(threadFactoryName, ThreadFactory.class);
} else {
threadFactory = Executors.defaultThreadFactory();
}
final String rejectedHandlerName = ConfigResolver.getPropertyValue(configPrefix + "rejectedExecutionHandler.name");
final RejectedExecutionHandler rejectedHandler;
if (rejectedHandlerName != null) {
rejectedHandler = lookupByName(rejectedHandlerName, RejectedExecutionHandler.class);
} else {
rejectedHandler = new ThreadPoolExecutor.AbortPolicy();
}
pool = new ThreadPoolExecutor(coreSize, maxSize, keepAlive, TimeUnit.valueOf(keepAliveUnit), queue, threadFactory, rejectedHandler);
}
pools.put(name, pool);
}
}
}
return pool;
}
use of java.util.concurrent.SynchronousQueue in project j2objc by google.
the class SynchronousQueueTest method testAddAll_ISE.
public void testAddAll_ISE(boolean fair) {
SynchronousQueue q = new SynchronousQueue(fair);
Integer[] ints = new Integer[1];
for (int i = 0; i < ints.length; i++) ints[i] = i;
Collection<Integer> coll = Arrays.asList(ints);
try {
q.addAll(coll);
shouldThrow();
} catch (IllegalStateException success) {
}
}
use of java.util.concurrent.SynchronousQueue in project j2objc by google.
the class SynchronousQueueTest method testClear.
public void testClear(boolean fair) {
final SynchronousQueue q = new SynchronousQueue(fair);
q.clear();
assertTrue(q.isEmpty());
}
Aggregations