Search in sources :

Example 71 with ThreadPoolExecutor

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

the class ConcurrentJobRequestsTestBase method executeJobOperations.

public void executeJobOperations(JobRunnable jobRunnable, int threadCount, boolean killThreads, boolean interruptThreads) throws IOException, InterruptedException, QueueException, NotAuthorizedException {
    started = false;
    ExecutorService executorService = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    ;
    ArrayList<Future<?>> futures = new ArrayList<Future<?>>();
    for (int i = 0; i < threadCount; i++) {
        futures.add(executorService.submit(jobRunnable));
    }
    waitForAllThreadsToStart(jobRunnable, threadCount);
    LOG.info("Started all threads ");
    if (killThreads) {
        executorService.shutdownNow();
    } else {
        if (interruptThreads) {
            for (Future<?> future : futures) {
                LOG.info("Cancelling the thread");
                future.cancel(true);
            }
        }
        executorService.shutdown();
    }
    /*
     * For both graceful or forceful shutdown, wait for tasks to terminate such that
     * appropriate exceptions are raised and stored in JobRunnable.exception.
     */
    if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
        LOG.info("Force Shutting down the pool\n");
        if (!killThreads) {
            /*
         * killThreads option has already done force shutdown. No need to do again.
         */
            executorService.shutdownNow();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 72 with ThreadPoolExecutor

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

the class TestExecutorService method testExecutorService.

@Test
public void testExecutorService() throws Exception {
    int maxThreads = 5;
    int maxTries = 10;
    int sleepInterval = 10;
    Server mockedServer = mock(Server.class);
    when(mockedServer.getConfiguration()).thenReturn(HBaseConfiguration.create());
    // Start an executor service pool with max 5 threads
    ExecutorService executorService = new ExecutorService("unit_test");
    executorService.startExecutorService(ExecutorType.MASTER_SERVER_OPERATIONS, maxThreads);
    Executor executor = executorService.getExecutor(ExecutorType.MASTER_SERVER_OPERATIONS);
    ThreadPoolExecutor pool = executor.threadPoolExecutor;
    // Assert no threads yet
    assertEquals(0, pool.getPoolSize());
    AtomicBoolean lock = new AtomicBoolean(true);
    AtomicInteger counter = new AtomicInteger(0);
    // Submit maxThreads executors.
    for (int i = 0; i < maxThreads; i++) {
        executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter));
    }
    // The TestEventHandler will increment counter when it starts.
    int tries = 0;
    while (counter.get() < maxThreads && tries < maxTries) {
        LOG.info("Waiting for all event handlers to start...");
        Thread.sleep(sleepInterval);
        tries++;
    }
    // Assert that pool is at max threads.
    assertEquals(maxThreads, counter.get());
    assertEquals(maxThreads, pool.getPoolSize());
    ExecutorStatus status = executor.getStatus();
    assertTrue(status.queuedEvents.isEmpty());
    assertEquals(5, status.running.size());
    checkStatusDump(status);
    // Now interrupt the running Executor
    synchronized (lock) {
        lock.set(false);
        lock.notifyAll();
    }
    // Executor increments counter again on way out so.... test that happened.
    while (counter.get() < (maxThreads * 2) && tries < maxTries) {
        System.out.println("Waiting for all event handlers to finish...");
        Thread.sleep(sleepInterval);
        tries++;
    }
    assertEquals(maxThreads * 2, counter.get());
    assertEquals(maxThreads, pool.getPoolSize());
    // Make sure we don't get RejectedExecutionException.
    for (int i = 0; i < (2 * maxThreads); i++) {
        executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter));
    }
    // Now interrupt the running Executor
    synchronized (lock) {
        lock.set(false);
        lock.notifyAll();
    }
    // Make sure threads are still around even after their timetolive expires.
    Thread.sleep(ExecutorService.Executor.keepAliveTimeInMillis * 2);
    assertEquals(maxThreads, pool.getPoolSize());
    executorService.shutdown();
    assertEquals(0, executorService.getAllExecutorStatuses().size());
    // Test that submit doesn't throw NPEs
    executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(org.apache.hadoop.hbase.executor.ExecutorService.Executor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ExecutorStatus(org.apache.hadoop.hbase.executor.ExecutorService.ExecutorStatus) Test(org.junit.Test)

Example 73 with ThreadPoolExecutor

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

the class ExecutorFactory method newThreadPool.

public static ExecutorService newThreadPool(int minThreads, int maxThreads, long maxIdleTime, TimeUnit unit, ThreadFactory threadFactory) {
    TaskQueue taskqueue = new TaskQueue();
    ThreadPoolExecutor service = new TribesThreadPoolExecutor(minThreads, maxThreads, maxIdleTime, unit, taskqueue, threadFactory);
    taskqueue.setParent(service);
    return service;
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 74 with ThreadPoolExecutor

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

the class ExecutorFactory method newThreadPool.

public static ExecutorService newThreadPool(int minThreads, int maxThreads, long maxIdleTime, TimeUnit unit) {
    TaskQueue taskqueue = new TaskQueue();
    ThreadPoolExecutor service = new TribesThreadPoolExecutor(minThreads, maxThreads, maxIdleTime, unit, taskqueue);
    taskqueue.setParent(service);
    return service;
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 75 with ThreadPoolExecutor

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

the class TestPartitionedMobCompactor method createThreadPool.

private static ExecutorService createThreadPool() {
    int maxThreads = 10;
    long keepAliveTime = 60;
    final SynchronousQueue<Runnable> queue = new SynchronousQueue<>();
    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, keepAliveTime, TimeUnit.SECONDS, queue, Threads.newDaemonThreadFactory("MobFileCompactionChore"), new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                // waiting for a thread to pick up instead of throwing exceptions.
                queue.put(r);
            } catch (InterruptedException e) {
                throw new RejectedExecutionException(e);
            }
        }
    });
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    return pool;
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

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