Search in sources :

Example 1 with ThreadPool

use of org.apache.dubbo.common.threadpool.ThreadPool in project dubbo by alibaba.

the class LimitedThreadPoolTest method getExecutor1.

@Test
public void getExecutor1() throws Exception {
    URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + THREAD_NAME_KEY + "=demo&" + CORE_THREADS_KEY + "=1&" + THREADS_KEY + "=2&" + QUEUES_KEY + "=0");
    ThreadPool threadPool = new LimitedThreadPool();
    ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
    assertThat(executor.getCorePoolSize(), is(1));
    assertThat(executor.getMaximumPoolSize(), is(2));
    assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(SynchronousQueue.class));
    assertThat(executor.getRejectedExecutionHandler(), Matchers.<RejectedExecutionHandler>instanceOf(AbortPolicyWithReport.class));
    final CountDownLatch latch = new CountDownLatch(1);
    executor.execute(new Runnable() {

        @Override
        public void run() {
            Thread thread = Thread.currentThread();
            assertThat(thread, instanceOf(InternalThread.class));
            assertThat(thread.getName(), startsWith("demo"));
            latch.countDown();
        }
    });
    latch.await();
    assertThat(latch.getCount(), is(0L));
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPool(org.apache.dubbo.common.threadpool.ThreadPool) AbortPolicyWithReport(org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) InternalThread(org.apache.dubbo.common.threadlocal.InternalThread) Test(org.junit.jupiter.api.Test)

Example 2 with ThreadPool

use of org.apache.dubbo.common.threadpool.ThreadPool in project dubbo by alibaba.

the class CachedThreadPoolTest method getExecutor1.

@Test
public void getExecutor1() throws Exception {
    URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + THREAD_NAME_KEY + "=demo&" + CORE_THREADS_KEY + "=1&" + THREADS_KEY + "=2&" + ALIVE_KEY + "=1000&" + QUEUES_KEY + "=0");
    ThreadPool threadPool = new CachedThreadPool();
    ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
    assertThat(executor.getCorePoolSize(), is(1));
    assertThat(executor.getMaximumPoolSize(), is(2));
    assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(SynchronousQueue.class));
    assertThat(executor.getRejectedExecutionHandler(), Matchers.<RejectedExecutionHandler>instanceOf(AbortPolicyWithReport.class));
    final CountDownLatch latch = new CountDownLatch(1);
    executor.execute(() -> {
        Thread thread = Thread.currentThread();
        assertThat(thread, instanceOf(InternalThread.class));
        assertThat(thread.getName(), startsWith("demo"));
        latch.countDown();
    });
    latch.await();
    assertThat(latch.getCount(), is(0L));
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPool(org.apache.dubbo.common.threadpool.ThreadPool) AbortPolicyWithReport(org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) InternalThread(org.apache.dubbo.common.threadlocal.InternalThread) InternalThread(org.apache.dubbo.common.threadlocal.InternalThread) Test(org.junit.jupiter.api.Test)

Example 3 with ThreadPool

use of org.apache.dubbo.common.threadpool.ThreadPool in project dubbo by alibaba.

the class CachedThreadPoolTest method getExecutor2.

@Test
public void getExecutor2() throws Exception {
    URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + QUEUES_KEY + "=1");
    ThreadPool threadPool = new CachedThreadPool();
    ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
    assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(LinkedBlockingQueue.class));
}
Also used : ThreadPool(org.apache.dubbo.common.threadpool.ThreadPool) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 4 with ThreadPool

use of org.apache.dubbo.common.threadpool.ThreadPool in project dubbo by alibaba.

the class EagerThreadPoolTest method getExecutor2.

@Test
public void getExecutor2() throws Exception {
    URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + QUEUES_KEY + "=2");
    ThreadPool threadPool = new EagerThreadPool();
    ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
    assertThat(executor.getQueue().remainingCapacity(), is(2));
}
Also used : ThreadPool(org.apache.dubbo.common.threadpool.ThreadPool) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 5 with ThreadPool

use of org.apache.dubbo.common.threadpool.ThreadPool in project dubbo by alibaba.

the class EagerThreadPoolTest method getExecutor1.

@Test
public void getExecutor1() throws Exception {
    URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + THREAD_NAME_KEY + "=demo&" + CORE_THREADS_KEY + "=1&" + THREADS_KEY + "=2&" + ALIVE_KEY + "=1000&" + QUEUES_KEY + "=0");
    ThreadPool threadPool = new EagerThreadPool();
    ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
    assertThat(executor, instanceOf(EagerThreadPoolExecutor.class));
    assertThat(executor.getCorePoolSize(), is(1));
    assertThat(executor.getMaximumPoolSize(), is(2));
    assertThat(executor.getKeepAliveTime(TimeUnit.MILLISECONDS), is(1000L));
    assertThat(executor.getQueue().remainingCapacity(), is(1));
    assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(TaskQueue.class));
    assertThat(executor.getRejectedExecutionHandler(), Matchers.<RejectedExecutionHandler>instanceOf(AbortPolicyWithReport.class));
    final CountDownLatch latch = new CountDownLatch(1);
    executor.execute(() -> {
        Thread thread = Thread.currentThread();
        assertThat(thread, instanceOf(InternalThread.class));
        assertThat(thread.getName(), startsWith("demo"));
        latch.countDown();
    });
    latch.await();
    assertThat(latch.getCount(), is(0L));
}
Also used : ThreadPool(org.apache.dubbo.common.threadpool.ThreadPool) AbortPolicyWithReport(org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) InternalThread(org.apache.dubbo.common.threadlocal.InternalThread) InternalThread(org.apache.dubbo.common.threadlocal.InternalThread) Test(org.junit.jupiter.api.Test)

Aggregations

ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)8 URL (org.apache.dubbo.common.URL)8 ThreadPool (org.apache.dubbo.common.threadpool.ThreadPool)8 Test (org.junit.jupiter.api.Test)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 InternalThread (org.apache.dubbo.common.threadlocal.InternalThread)4 AbortPolicyWithReport (org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 SynchronousQueue (java.util.concurrent.SynchronousQueue)3