Search in sources :

Example 51 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project jPOS by jpos.

the class TSpacePerformanceTest method testDeadLockWithNotify.

@Test
public void testDeadLockWithNotify() throws Throwable {
    int size = 10;
    final ExecutorService es = new ThreadPoolExecutor(size * 2, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue());
    ((ThreadPoolExecutor) es).prestartAllCoreThreads();
    for (int i = 0; i < size; i++) es.execute(new WriteSpaceWithNotifyTask("WriteTask1-" + i, sp1, sp2));
    for (int i = 0; i < size; i++) es.execute(new WriteSpaceWithNotifyTask("WriteTask2-" + i, sp2, sp1));
    long stamp = System.currentTimeMillis();
    while (((ThreadPoolExecutor) es).getActiveCount() > 0) {
        if (System.currentTimeMillis() - stamp < 10000) {
            ISOUtil.sleep(100);
            continue;
        }
        es.shutdownNow();
        fail("Probably death-lock detected");
        return;
    }
    printAvg(t1, "Avg. write: ");
    // es.shutdown();
    es.shutdownNow();
    es.awaitTermination(5, TimeUnit.SECONDS);
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 52 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project rt.equinox.framework by eclipse.

the class TestModuleContainer method testUsesTimeout.

// DISABLE see bug 498064 @Test
public void testUsesTimeout() throws BundleException {
    // Always want to go to zero threads when idle
    int coreThreads = 0;
    // use the number of processors - 1 because we use the current thread when rejected
    int maxThreads = Math.max(Runtime.getRuntime().availableProcessors() - 1, 1);
    // idle timeout; make it short to get rid of threads quickly after resolve
    int idleTimeout = 5;
    // use sync queue to force thread creation
    BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();
    // try to name the threads with useful name
    ThreadFactory threadFactory = new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            // $NON-NLS-1$
            Thread t = new Thread(r, "Resolver thread - UNIT TEST");
            t.setDaemon(true);
            return t;
        }
    };
    // use a rejection policy that simply runs the task in the current thread once the max threads is reached
    RejectedExecutionHandler rejectHandler = new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor exe) {
            r.run();
        }
    };
    ExecutorService executor = new ThreadPoolExecutor(coreThreads, maxThreads, idleTimeout, TimeUnit.SECONDS, queue, threadFactory, rejectHandler);
    ScheduledExecutorService timeoutExecutor = new ScheduledThreadPoolExecutor(1);
    Map<String, String> configuration = new HashMap<String, String>();
    configuration.put(EquinoxConfiguration.PROP_RESOLVER_BATCH_TIMEOUT, "5000");
    Map<String, String> debugOpts = Collections.emptyMap();
    DummyContainerAdaptor adaptor = new DummyContainerAdaptor(new DummyCollisionHook(false), configuration, new DummyResolverHookFactory(), new DummyDebugOptions(debugOpts));
    adaptor.setResolverExecutor(executor);
    adaptor.setTimeoutExecutor(timeoutExecutor);
    ModuleContainer container = adaptor.getContainer();
    for (int i = 1; i <= 1000; i++) {
        for (Map<String, String> manifest : getUsesTimeoutManifests("test" + i)) {
            installDummyModule(manifest, manifest.get(Constants.BUNDLE_SYMBOLICNAME), container);
        }
    }
    ResolutionReport report = container.resolve(container.getModules(), true);
    Assert.assertNull("Found resolution errors.", report.getResolutionException());
    for (Module module : container.getModules()) {
        Assert.assertEquals("Wrong state of module: " + module, State.RESOLVED, module.getState());
    }
    executor.shutdown();
    timeoutExecutor.shutdown();
    System.gc();
    System.gc();
    System.gc();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) HashMap(java.util.HashMap) DummyCollisionHook(org.eclipse.osgi.tests.container.dummys.DummyCollisionHook) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) DummyDebugOptions(org.eclipse.osgi.tests.container.dummys.DummyDebugOptions) ResolutionReport(org.eclipse.osgi.report.resolution.ResolutionReport) DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) SynchronousQueue(java.util.concurrent.SynchronousQueue) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Module(org.eclipse.osgi.container.Module) DummyResolverHookFactory(org.eclipse.osgi.tests.container.dummys.DummyResolverHookFactory)

Example 53 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project rt.equinox.framework by eclipse.

the class EquinoxContainerAdaptor method createLazyExecutorCreator.

private Callable<Executor> createLazyExecutorCreator(EquinoxConfiguration config) {
    String threadCntProp = config.getConfiguration(EquinoxConfiguration.PROP_RESOLVER_THREAD_COUNT);
    int threadCntTmp;
    try {
        threadCntTmp = threadCntProp == null ? -1 : Integer.parseInt(threadCntProp);
    } catch (NumberFormatException e) {
        threadCntTmp = -1;
    }
    // use the number of processors - 1 because we use the current thread when rejected
    final int maxThreads = threadCntTmp <= 0 ? Math.max(Runtime.getRuntime().availableProcessors() - 1, 1) : threadCntTmp;
    return new Callable<Executor>() {

        @Override
        public Executor call() throws Exception {
            if (maxThreads == 1) {
                return new Executor() {

                    @Override
                    public void execute(Runnable command) {
                        command.run();
                    }
                };
            }
            // Always want to go to zero threads when idle
            int coreThreads = 0;
            // idle timeout; make it short to get rid of threads quickly after resolve
            int idleTimeout = 10;
            // use sync queue to force thread creation
            BlockingQueue<Runnable> queue = new SynchronousQueue<>();
            // try to name the threads with useful name
            ThreadFactory threadFactory = new ThreadFactory() {

                @Override
                public Thread newThread(Runnable r) {
                    // $NON-NLS-1$
                    Thread t = new Thread(r, "Resolver thread - " + EquinoxContainerAdaptor.this.toString());
                    t.setDaemon(true);
                    return t;
                }
            };
            // use a rejection policy that simply runs the task in the current thread once the max threads is reached
            RejectedExecutionHandler rejectHandler = new RejectedExecutionHandler() {

                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor exe) {
                    r.run();
                }
            };
            return new ThreadPoolExecutor(coreThreads, maxThreads, idleTimeout, TimeUnit.SECONDS, queue, threadFactory, rejectHandler);
        }
    };
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Callable(java.util.concurrent.Callable)

Example 54 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project Payara by payara.

the class EjbContainerUtilImpl method createThreadPoolExecutor.

private ThreadPoolExecutor createThreadPoolExecutor(String poolName) {
    ThreadPoolExecutor result = null;
    String val = ejbContainer.getPropertyValue(RuntimeTagNames.THREAD_CORE_POOL_SIZE);
    int corePoolSize = val != null ? Integer.parseInt(val.trim()) : EjbContainer.DEFAULT_THREAD_CORE_POOL_SIZE;
    val = ejbContainer.getPropertyValue(RuntimeTagNames.THREAD_MAX_POOL_SIZE);
    int maxPoolSize = val != null ? Integer.parseInt(val.trim()) : EjbContainer.DEFAULT_THREAD_MAX_POOL_SIZE;
    val = ejbContainer.getPropertyValue(RuntimeTagNames.THREAD_KEEP_ALIVE_SECONDS);
    long keepAliveSeconds = val != null ? Long.parseLong(val.trim()) : EjbContainer.DEFAULT_THREAD_KEEP_ALIVE_SECONDS;
    val = ejbContainer.getPropertyValue(RuntimeTagNames.THREAD_QUEUE_CAPACITY);
    int queueCapacity = val != null ? Integer.parseInt(val.trim()) : EjbContainer.DEFAULT_THREAD_QUEUE_CAPACITY;
    val = ejbContainer.getPropertyValue(RuntimeTagNames.ALLOW_CORE_THREAD_TIMEOUT);
    boolean allowCoreThreadTimeout = val != null ? Boolean.parseBoolean(val.trim()) : EjbContainer.DEFAULT_ALLOW_CORE_THREAD_TIMEOUT;
    val = ejbContainer.getPropertyValue(RuntimeTagNames.PRESTART_ALL_CORE_THREADS);
    boolean preStartAllCoreThreads = val != null ? Boolean.parseBoolean(val.trim()) : EjbContainer.DEFAULT_PRESTART_ALL_CORE_THREADS;
    BlockingQueue workQueue = queueCapacity > 0 ? new LinkedBlockingQueue<Runnable>(queueCapacity) : new SynchronousQueue(true);
    // PAYARA-405 validates attributes of the thread pool to ensure no problems
    if (corePoolSize < 0) {
        _logger.log(Level.WARNING, "Core Pool Size configured to be less than 0. Resetting to 0");
        corePoolSize = 0;
    }
    if (maxPoolSize < 1) {
        _logger.log(Level.WARNING, "Max Pool Size configured to be less than 1. Resetting to 1");
        maxPoolSize = 1;
    }
    if (corePoolSize > maxPoolSize) {
        _logger.log(Level.WARNING, "Core Pool Size configured to be greater than maxPoolSize. Resetting to maxPoolSize {0}", maxPoolSize);
        corePoolSize = maxPoolSize;
    }
    if (keepAliveSeconds < 0) {
        _logger.log(Level.WARNING, "Keep Alive Seconds configured to be less than 0. Resetting to 0");
        keepAliveSeconds = 0;
    }
    result = new EjbThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, workQueue, poolName);
    if (allowCoreThreadTimeout) {
        result.allowCoreThreadTimeOut(true);
    }
    if (preStartAllCoreThreads) {
        result.prestartAllCoreThreads();
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Created {0}", result.toString());
    }
    return result;
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 55 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project Payara by payara.

the class PayaraExecutorService method initialiseThreadPools.

private void initialiseThreadPools() {
    int threadPoolExecutorQueueSize = Integer.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorQueueSize());
    if (threadPoolExecutorQueueSize > 0) {
        threadPoolExecutor = new ThreadPoolExecutor(Integer.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorCorePoolSize()), Integer.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorMaxPoolSize()), Integer.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorKeepAliveTime()), TimeUnit.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorKeepAliveTimeUnit()), new LinkedBlockingQueue<>(threadPoolExecutorQueueSize), (Runnable r) -> new Thread(r, "payara-executor-service-task"));
    } else {
        threadPoolExecutor = new ThreadPoolExecutor(Integer.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorCorePoolSize()), Integer.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorMaxPoolSize()), Integer.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorKeepAliveTime()), TimeUnit.valueOf(payaraExecutorServiceConfiguration.getThreadPoolExecutorKeepAliveTimeUnit()), new SynchronousQueue<>(), (Runnable r) -> new Thread(r, "payara-executor-service-task"));
    }
    scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(Integer.valueOf(payaraExecutorServiceConfiguration.getScheduledThreadPoolExecutorCorePoolSize()));
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Aggregations

SynchronousQueue (java.util.concurrent.SynchronousQueue)55 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)39 ExecutorService (java.util.concurrent.ExecutorService)9 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)7 ThreadFactory (java.util.concurrent.ThreadFactory)7 XMPPException (org.jivesoftware.smack.XMPPException)7 Test (org.junit.Test)7 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 IOException (java.io.IOException)5 BlockingQueue (java.util.concurrent.BlockingQueue)5 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 ArrayList (java.util.ArrayList)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 AssignmentManager (org.apache.hadoop.hbase.master.AssignmentManager)4 ExecutorUtil (org.apache.solr.common.util.ExecutorUtil)4 DefaultSolrThreadFactory (org.apache.solr.util.DefaultSolrThreadFactory)4