Search in sources :

Example 36 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project WordPress-Android by wordpress-mobile.

the class StatsFollowersFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Single background thread used to create the blogs list in BG
    ThreadPoolExecutor blogsListCreatorExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
    blogsListCreatorExecutor.submit(new Thread() {

        @Override
        public void run() {
            // Read all the .com and jetpack sites and get the list of home URLs.
            // This will be used later to check if the user is a member of followers blog marked as private.
            List<SiteModel> sites = mSiteStore.getWPComAndJetpackSites();
            for (SiteModel site : sites) {
                if (site.getUrl() != null && site.getSiteId() != 0) {
                    String normURL = normalizeAndRemoveScheme(site.getUrl());
                    long blogID = site.getSiteId();
                    userBlogs.put(normURL, blogID);
                }
            }
        }
    });
}
Also used : List(java.util.List) SiteModel(org.wordpress.android.fluxc.model.SiteModel) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 37 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project hazelcast by hazelcast.

the class DelegateAndSkipOnConcurrentExecutionDecoratorTest method givenTheTaskIsNotRunning_whenThreadAttemptToExecuteIt_theTaskWillBeExecuted.

@Test
public void givenTheTaskIsNotRunning_whenThreadAttemptToExecuteIt_theTaskWillBeExecuted() throws InterruptedException {
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    ResumableCountingRunnable task = new ResumableCountingRunnable();
    decorateAndInvokeRunOnDifferentThread(task, executor);
    task.awaitExecutionStarted();
    task.resumeExecution();
    assertEquals(1, task.getExecutionCount());
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 38 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project hazelcast by hazelcast.

the class DelegateAndSkipOnConcurrentExecutionDecoratorTest method givenTheTaskIsAlreadyRunning_whenThreadAttemptToExecuteIt_theExutionWillBeSkipped.

@Test
public void givenTheTaskIsAlreadyRunning_whenThreadAttemptToExecuteIt_theExutionWillBeSkipped() throws InterruptedException {
    final ResumableCountingRunnable task = new ResumableCountingRunnable();
    final AtomicInteger counter = new AtomicInteger();
    SynchronousQueue<Runnable> queue = new SynchronousQueue<Runnable>() {

        @Override
        public boolean offer(Runnable runnable) {
            counter.incrementAndGet();
            return super.offer(runnable);
        }
    };
    ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, queue);
    //start first task
    DelegateAndSkipOnConcurrentExecutionDecorator decoratedTask = decorateAndInvokeRunOnDifferentThread(task, executor);
    //wait until the task is running
    task.awaitExecutionStarted();
    //attempt to start execution from the test thread. this execution should be skipped -> it won't block
    decoratedTask.run();
    //resume the original task
    task.resumeExecution();
    assertEquals(1, task.getExecutionCount());
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 39 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project spring-framework by spring-projects.

the class ThreadPoolExecutorFactoryBean method initializeExecutor.

@Override
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
    BlockingQueue<Runnable> queue = createQueue(this.queueCapacity);
    ThreadPoolExecutor executor = createExecutor(this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, queue, threadFactory, rejectedExecutionHandler);
    if (this.allowCoreThreadTimeOut) {
        executor.allowCoreThreadTimeOut(true);
    }
    // Wrap executor with an unconfigurable decorator.
    this.exposedExecutor = (this.exposeUnconfigurableExecutor ? Executors.unconfigurableExecutorService(executor) : executor);
    return executor;
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 40 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project spring-framework by spring-projects.

the class ThreadPoolTaskExecutor method initializeExecutor.

/**
	 * Note: This method exposes an {@link ExecutorService} to its base class
	 * but stores the actual {@link ThreadPoolExecutor} handle internally.
	 * Do not override this method for replacing the executor, rather just for
	 * decorating its {@code ExecutorService} handle or storing custom state.
	 */
@Override
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
    BlockingQueue<Runnable> queue = createQueue(this.queueCapacity);
    ThreadPoolExecutor executor;
    if (this.taskDecorator != null) {
        executor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler) {

            @Override
            public void execute(Runnable command) {
                super.execute(taskDecorator.decorate(command));
            }
        };
    } else {
        executor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler);
    }
    if (this.allowCoreThreadTimeOut) {
        executor.allowCoreThreadTimeOut(true);
    }
    this.threadPoolExecutor = executor;
    return executor;
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

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