Search in sources :

Example 31 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project bazel by bazelbuild.

the class AbstractQueueVisitorTest method interruptionWithInterruptingWorkers.

@Test
public void interruptionWithInterruptingWorkers() throws Exception {
    assertInterruptWorkers(null);
    ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
    assertInterruptWorkers(executor);
    executor.shutdown();
    executor.awaitTermination(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 32 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project camel by apache.

the class SpringCamelContextThreadPoolProfilesTest method testBigProfile.

public void testBigProfile() throws Exception {
    CamelContext context = getMandatoryBean(CamelContext.class, "camel-C");
    ThreadPoolProfile profile = context.getExecutorServiceManager().getThreadPoolProfile("big");
    assertEquals(50, profile.getPoolSize().intValue());
    assertEquals(100, profile.getMaxPoolSize().intValue());
    assertEquals(ThreadPoolRejectedPolicy.DiscardOldest, profile.getRejectedPolicy());
    assertEquals(null, profile.getKeepAliveTime());
    assertEquals(null, profile.getMaxQueueSize());
    // create a thread pool from big
    ExecutorService executor = context.getExecutorServiceManager().newThreadPool(this, "MyBig", "big");
    ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, executor);
    assertEquals(50, tp.getCorePoolSize());
    assertEquals(100, tp.getMaximumPoolSize());
    // should inherit default options
    assertEquals(60, tp.getKeepAliveTime(TimeUnit.SECONDS));
    assertEquals("DiscardOldest", tp.getRejectedExecutionHandler().toString());
}
Also used : CamelContext(org.apache.camel.CamelContext) ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 33 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project camel by apache.

the class SpringCamelContextThreadPoolProfilesTest method testLowProfile.

public void testLowProfile() throws Exception {
    CamelContext context = getMandatoryBean(CamelContext.class, "camel-C");
    ThreadPoolProfile profile = context.getExecutorServiceManager().getThreadPoolProfile("low");
    assertEquals(1, profile.getPoolSize().intValue());
    assertEquals(5, profile.getMaxPoolSize().intValue());
    assertEquals(null, profile.getKeepAliveTime());
    assertEquals(null, profile.getMaxQueueSize());
    assertEquals(null, profile.getRejectedPolicy());
    // create a thread pool from low
    ExecutorService executor = context.getExecutorServiceManager().newThreadPool(this, "MyLow", "low");
    ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, executor);
    assertEquals(1, tp.getCorePoolSize());
    assertEquals(5, tp.getMaximumPoolSize());
    // should inherit default options
    assertEquals(60, tp.getKeepAliveTime(TimeUnit.SECONDS));
    assertEquals("CallerRuns", tp.getRejectedExecutionHandler().toString());
}
Also used : CamelContext(org.apache.camel.CamelContext) ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 34 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project jedis by xetorthio.

the class JedisClusterTest method testJedisClusterRunsWithMultithreaded.

@Test
public void testJedisClusterRunsWithMultithreaded() throws InterruptedException, ExecutionException, IOException {
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    final JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
    jc.set("foo", "bar");
    ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
    List<Future<String>> futures = new ArrayList<Future<String>>();
    for (int i = 0; i < 50; i++) {
        executor.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                // random connection also does work
                return jc.get("foo");
            }
        });
    }
    for (Future<String> future : futures) {
        String value = future.get();
        assertEquals("bar", value);
    }
    jc.close();
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 35 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project android-priority-jobqueue by yigit.

the class MultiThreadTest method testMultiThreaded.

@Test
public void testMultiThreaded() throws Exception {
    multiThreadedJobCounter = new AtomicInteger(0);
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).loadFactor(3).maxConsumerCount(10));
    int limit = 200;
    ExecutorService executor = new ThreadPoolExecutor(20, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(limit));
    final String cancelTag = "iWillBeCancelled";
    Collection<Future<?>> futures = new LinkedList<Future<?>>();
    for (int i = 0; i < limit; i++) {
        final int id = i;
        futures.add(executor.submit(new Runnable() {

            @Override
            public void run() {
                final boolean persistent = Math.round(Math.random()) % 2 == 0;
                boolean requiresNetwork = Math.round(Math.random()) % 2 == 0;
                int priority = (int) (Math.round(Math.random()) % 10);
                multiThreadedJobCounter.incrementAndGet();
                Params params = new Params(priority).setRequiresNetwork(requiresNetwork).setPersistent(persistent);
                if (Math.random() < .1) {
                    params.addTags(cancelTag);
                }
                jobManager.addJob(new DummyJobForMultiThread(id, params));
            }
        }));
    }
    // wait for some jobs to start
    //noinspection SLEEP_IN_CODE
    Thread.sleep(1000);
    CancelResult cancelResult = jobManager.cancelJobs(TagConstraint.ALL, cancelTag);
    for (int i = 0; i < cancelResult.getCancelledJobs().size(); i++) {
        multiThreadedJobCounter.decrementAndGet();
    }
    for (Future<?> future : futures) {
        future.get();
    }
    Log.d("TAG", "added all jobs");
    //wait until all jobs are added
    //noinspection DIRECT_TIME_ACCESS
    long start = System.nanoTime();
    //20 minutes
    long timeLimit = JobManager.NS_PER_MS * 60000 * 20;
    //noinspection DIRECT_TIME_ACCESS
    while (System.nanoTime() - start < timeLimit && multiThreadedJobCounter.get() != 0) {
        //noinspection SLEEP_IN_CODE
        Thread.sleep(1000);
    }
    MatcherAssert.assertThat("jobmanager count should be 0", jobManager.count(), equalTo(0));
    jobManager.stopAndWaitUntilConsumersAreFinished();
    MatcherAssert.assertThat("multiThreadedJobCounter should be 0", multiThreadedJobCounter.get(), CoreMatchers.is(0));
}
Also used : CancelResult(com.birbit.android.jobqueue.CancelResult) Configuration(com.birbit.android.jobqueue.config.Configuration) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) TagConstraint(com.birbit.android.jobqueue.TagConstraint) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Aggregations

ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)441 Test (org.junit.Test)87 ExecutorService (java.util.concurrent.ExecutorService)79 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)66 ThreadFactory (java.util.concurrent.ThreadFactory)45 SynchronousQueue (java.util.concurrent.SynchronousQueue)38 IOException (java.io.IOException)37 ArrayList (java.util.ArrayList)36 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)27 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)26 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)25 CountDownLatch (java.util.concurrent.CountDownLatch)25 ExecutionException (java.util.concurrent.ExecutionException)25 Future (java.util.concurrent.Future)23 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)19 Test (org.testng.annotations.Test)18 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 HashMap (java.util.HashMap)16 SizedScheduledExecutorService (org.apache.camel.util.concurrent.SizedScheduledExecutorService)16