Search in sources :

Example 1 with AbortPolicyWithReport

use of com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport in project dubbo by alibaba.

the class FixedThreadPool method getExecutor.

public Executor getExecutor(URL url) {
    String name = url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME);
    int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS);
    int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
    return new ThreadPoolExecutor(threads, threads, 0, 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 2 with AbortPolicyWithReport

use of com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport in project dubbo by alibaba.

the class LimitedThreadPool 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, Constants.DEFAULT_THREADS);
    int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
    return new ThreadPoolExecutor(cores, threads, Long.MAX_VALUE, 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 3 with AbortPolicyWithReport

use of com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport in project dubbo by alibaba.

the class AbortPolicyWithReportTest method jStackDumpTest.

@Test
public void jStackDumpTest() throws InterruptedException {
    URL url = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?dump.directory=/tmp&version=1.0.0&application=morgan&noValue");
    AbortPolicyWithReport abortPolicyWithReport = new AbortPolicyWithReport("Test", url);
    try {
        abortPolicyWithReport.rejectedExecution(new Runnable() {

            @Override
            public void run() {
                System.out.println("hello");
            }
        }, (ThreadPoolExecutor) Executors.newFixedThreadPool(1));
    } catch (RejectedExecutionException rj) {
    }
    Thread.currentThread().sleep(1000);
}
Also used : AbortPolicyWithReport(com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport) URL(com.alibaba.dubbo.common.URL) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Example 4 with AbortPolicyWithReport

use of com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport 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 5 with AbortPolicyWithReport

use of com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport in project Hotchpotch by carryxyh.

the class EnhancedThreadPoolTest method testEnhancedThreadPool.

/**
 * it print like this:
 * thread number in current pool:1,  task number in task queue:0 executor size: 1
 * thread number in current pool:2,  task number in task queue:0 executor size: 2
 * thread number in current pool:3,  task number in task queue:0 executor size: 3
 * thread number in current pool:4,  task number in task queue:0 executor size: 4
 * thread number in current pool:5,  task number in task queue:0 executor size: 5
 * thread number in current pool:6,  task number in task queue:0 executor size: 6
 * thread number in current pool:7,  task number in task queue:0 executor size: 7
 * thread number in current pool:8,  task number in task queue:0 executor size: 8
 * thread number in current pool:9,  task number in task queue:0 executor size: 9
 * thread number in current pool:10,  task number in task queue:0 executor size: 10
 * thread number in current pool:10,  task number in task queue:4 executor size: 10
 * thread number in current pool:10,  task number in task queue:3 executor size: 10
 * thread number in current pool:10,  task number in task queue:2 executor size: 10
 * thread number in current pool:10,  task number in task queue:1 executor size: 10
 * thread number in current pool:10,  task number in task queue:0 executor size: 10
 *
 * we can see , when the core threads are in busy , the thread pool create thread (but thread nums always less than max) instead of put task into queue.
 */
@Test
public void testEnhancedThreadPool() throws Exception {
    String name = "enhanced-tf";
    int queues = 5;
    int cores = 5;
    int threads = 10;
    // alive 1 second
    long alive = 1000;
    URL url = new URL("dubbo", "localhost", 8080);
    // init queue and enhanced executor
    EnhancedTaskQueue<Runnable> enhancedTaskQueue = new EnhancedTaskQueue<Runnable>(queues);
    final EnhancedThreadPoolExecutor executor = new EnhancedThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, enhancedTaskQueue, new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url));
    enhancedTaskQueue.setExecutor(executor);
    for (int i = 0; i < 15; i++) {
        Thread.sleep(50);
        executor.execute(new Runnable() {

            @Override
            public void run() {
                System.out.println("thread number in current pool:" + executor.getPoolSize() + ",  task number in task queue:" + executor.getQueue().size() + " executor size: " + executor.getPoolSize());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    Thread.sleep(5000);
    // cores theads are all alive
    Assert.assertTrue("more than cores threads alive!", executor.getPoolSize() == cores);
}
Also used : NamedThreadFactory(com.alibaba.dubbo.common.utils.NamedThreadFactory) AbortPolicyWithReport(com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Aggregations

AbortPolicyWithReport (com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport)5 NamedThreadFactory (com.alibaba.dubbo.common.utils.NamedThreadFactory)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 URL (com.alibaba.dubbo.common.URL)2 Test (org.junit.Test)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1