Search in sources :

Example 26 with SynchronousQueue

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);
}
Also used : TTransportFactory(org.apache.storm.thrift.transport.TTransportFactory) ExtendedThreadPoolExecutor(org.apache.storm.utils.ExtendedThreadPoolExecutor) TServerSocket(org.apache.storm.thrift.transport.TServerSocket) TBinaryProtocol(org.apache.storm.thrift.protocol.TBinaryProtocol) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ExtendedThreadPoolExecutor(org.apache.storm.utils.ExtendedThreadPoolExecutor) TThreadPoolServer(org.apache.storm.thrift.server.TThreadPoolServer)

Example 27 with SynchronousQueue

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 28 with SynchronousQueue

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;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InitialContext(javax.naming.InitialContext) Bean(javax.enterprise.inject.spi.Bean) SynchronousQueue(java.util.concurrent.SynchronousQueue) ExecutorService(java.util.concurrent.ExecutorService) NamingException(javax.naming.NamingException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 29 with SynchronousQueue

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) {
    }
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue)

Example 30 with SynchronousQueue

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());
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue)

Aggregations

SynchronousQueue (java.util.concurrent.SynchronousQueue)117 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)61 ExecutorService (java.util.concurrent.ExecutorService)20 ThreadFactory (java.util.concurrent.ThreadFactory)14 ArrayList (java.util.ArrayList)12 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)12 IOException (java.io.IOException)9 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)9 Test (org.junit.Test)9 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 BlockingQueue (java.util.concurrent.BlockingQueue)7 XMPPException (org.jivesoftware.smack.XMPPException)7 Future (java.util.concurrent.Future)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 List (java.util.List)4