Search in sources :

Example 1 with PriorityThreadPoolExecutor

use of jgnash.engine.concurrent.PriorityThreadPoolExecutor in project jgnash by ccavanaugh.

the class AbstractJpaDAO method shutDownExecutor.

static void shutDownExecutor() {
    // Stop the shared executor server, wait for all tasks to complete
    emLock.lock();
    try {
        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        // Regenerate the executor service
        executorService = new PriorityThreadPoolExecutor();
    } catch (final InterruptedException e) {
        logSevere(AbstractJpaDAO.class, e);
        Thread.currentThread().interrupt();
    } finally {
        emLock.unlock();
    }
}
Also used : PriorityThreadPoolExecutor(jgnash.engine.concurrent.PriorityThreadPoolExecutor)

Example 2 with PriorityThreadPoolExecutor

use of jgnash.engine.concurrent.PriorityThreadPoolExecutor in project jgnash by ccavanaugh.

the class PriorityThreadTest method testPriorityThreadPoolExecutor.

@Test
void testPriorityThreadPoolExecutor() throws Exception {
    final AtomicLong atomicLongSequence = new AtomicLong(0);
    final PriorityThreadPoolExecutor executorService = new PriorityThreadPoolExecutor();
    final List<Future<Void>> futures = new ArrayList<>();
    for (int i = 0; i < FUTURES; i++) {
        Future<Void> future = executorService.submit(() -> {
            final long value = atomicLongSequence.incrementAndGet();
            Thread.sleep(DELAY_MILLIS);
            System.out.println("Background Callable: " + value);
            return null;
        }, Priority.BACKGROUND);
        futures.add(future);
    }
    Thread.sleep(1999);
    final Future<Void> priorityFuture = executorService.submit(() -> {
        final long value = atomicLongSequence.incrementAndGet();
        Thread.sleep(DELAY_MILLIS);
        System.out.println("System Callable: " + value);
        // should not be the last value
        assertTrue(value < (FUTURES / 2));
        return null;
    });
    futures.add(priorityFuture);
    // wait for futures to complete
    for (Future<Void> future : futures) {
        future.get();
    }
    executorService.shutdown();
    executorService.shutdownNow();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) PriorityThreadPoolExecutor(jgnash.engine.concurrent.PriorityThreadPoolExecutor) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Aggregations

PriorityThreadPoolExecutor (jgnash.engine.concurrent.PriorityThreadPoolExecutor)2 ArrayList (java.util.ArrayList)1 Future (java.util.concurrent.Future)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.jupiter.api.Test)1