Search in sources :

Example 76 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 77 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project dubbo by alibaba.

the class CachedThreadPool method getExecutor.

public Executor getExecutor(URL url) {
    String name = url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME);
    int cores = url.getParameter(Constants.CORE_THREADS_KEY, Constants.DEFAULT_CORE_THREADS);
    int threads = url.getParameter(Constants.THREADS_KEY, Integer.MAX_VALUE);
    int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
    int alive = url.getParameter(Constants.ALIVE_KEY, Constants.DEFAULT_ALIVE);
    return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, queues == 0 ? new SynchronousQueue<Runnable>() : (queues < 0 ? new LinkedBlockingQueue<Runnable>() : new LinkedBlockingQueue<Runnable>(queues)), new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url));
}
Also used : NamedThreadFactory(com.alibaba.dubbo.common.utils.NamedThreadFactory) SynchronousQueue(java.util.concurrent.SynchronousQueue) AbortPolicyWithReport(com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 78 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project CommandHelper by EngineHub.

the class ExecutionQueue method startQueue.

private synchronized void startQueue(final DaemonManager dm, final String queue) {
    synchronized (locks.get(queue)) {
        if (!isRunning(queue)) {
            // We need to create a new thread
            runningQueues.put(queue, true);
            if (dm != null) {
                dm.activateThread(null);
            }
            if (service == null) {
                service = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 50L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), threadFactory);
            }
            service.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        pumpQueue(queue);
                    } catch (RuntimeException t) {
                        if (uncaughtExceptionHandler != null) {
                            uncaughtExceptionHandler.uncaughtException(Thread.currentThread(), t);
                        } else {
                            StreamUtils.GetSystemErr().println("The queue \"" + queue + "\" threw an exception, and it was not handled.");
                            t.printStackTrace(StreamUtils.GetSystemErr());
                        }
                    } finally {
                        if (dm != null) {
                            dm.deactivateThread(null);
                        }
                    }
                }
            });
        }
    }
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 79 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project cdap by caskdata.

the class HBaseMetricsTable method initializeV3Vars.

private void initializeV3Vars(CConfiguration cConf, DatasetSpecification spec) {
    boolean isV3Table = spec.getName().contains("v3");
    this.scanExecutor = null;
    this.rowKeyDistributor = null;
    if (isV3Table) {
        RejectedExecutionHandler callerRunsPolicy = new RejectedExecutionHandler() {

            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                REJECTION_LOG.info("No more threads in the HBase scan thread pool. Consider increase {}. Performing scan in caller thread {}", Constants.Metrics.METRICS_HBASE_MAX_SCAN_THREADS, Thread.currentThread().getName());
                // Runs it from the caller thread
                if (!executor.isShutdown()) {
                    r.run();
                }
            }
        };
        int maxScanThread = cConf.getInt(Constants.Metrics.METRICS_HBASE_MAX_SCAN_THREADS);
        // Creates a executor that will shrink to 0 threads if left idle
        // Uses daemon thread, hence no need to worry about shutdown
        // When all threads are busy, use the caller thread to execute
        this.scanExecutor = new ThreadPoolExecutor(0, maxScanThread, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), Threads.createDaemonThreadFactory("metrics-hbase-scanner-%d"), callerRunsPolicy);
        this.rowKeyDistributor = new RowKeyDistributorByHashPrefix(new RowKeyDistributorByHashPrefix.OneByteSimpleHash(spec.getIntProperty(Constants.Metrics.METRICS_HBASE_TABLE_SPLITS, 16)));
    }
}
Also used : RowKeyDistributorByHashPrefix(co.cask.cdap.hbase.wd.RowKeyDistributorByHashPrefix) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 80 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project activemq-artemis by apache.

the class JournalImpl method start.

@Override
public synchronized void start() {
    if (state != JournalState.STOPPED) {
        throw new IllegalStateException("Journal " + this + " is not stopped, state is " + state);
    }
    if (providedIOThreadPool == null) {
        ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {

            @Override
            public ThreadFactory run() {
                return new ActiveMQThreadFactory("ArtemisIOThread", true, JournalImpl.class.getClassLoader());
            }
        });
        threadPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), factory);
        ioExecutorFactory = new OrderedExecutorFactory(threadPool);
    } else {
        ioExecutorFactory = providedIOThreadPool;
    }
    filesExecutor = ioExecutorFactory.getExecutor();
    compactorExecutor = ioExecutorFactory.getExecutor();
    appendExecutor = ioExecutorFactory.getExecutor();
    filesRepository.setExecutor(filesExecutor);
    fileFactory.start();
    setJournalState(JournalState.STARTED);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ActiveMQThreadFactory(org.apache.activemq.artemis.utils.ActiveMQThreadFactory) OrderedExecutorFactory(org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory) SynchronousQueue(java.util.concurrent.SynchronousQueue) ActiveMQThreadFactory(org.apache.activemq.artemis.utils.ActiveMQThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

SynchronousQueue (java.util.concurrent.SynchronousQueue)120 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)64 ExecutorService (java.util.concurrent.ExecutorService)21 ThreadFactory (java.util.concurrent.ThreadFactory)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)13 ArrayList (java.util.ArrayList)12 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)10 IOException (java.io.IOException)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