Search in sources :

Example 51 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project jdk8u_jdk by JetBrains.

the class SpliteratorTraverseAddRemoveTest method testQueue.

@Test(dataProvider = "spliteratorTraversers")
public void testQueue(String desc, Consumer<Queue<String>> c) throws InterruptedException {
    AtomicBoolean done = new AtomicBoolean(false);
    Queue<String> msgs = new LinkedTransferQueue<>();
    CompletableFuture<Void> traversalTask = CompletableFuture.runAsync(() -> {
        while (!done.get()) {
            // Traversal will fail if self-linked nodes of
            // LinkedTransferQueue are erroneously reported
            c.accept(msgs);
        }
    });
    CompletableFuture<Void> addAndRemoveTask = CompletableFuture.runAsync(() -> {
        while (!traversalTask.isDone()) {
            msgs.add("msg");
            msgs.remove("msg");
        }
    });
    Thread.sleep(TimeUnit.SECONDS.toMillis(1));
    done.set(true);
    addAndRemoveTask.join();
    Assert.assertTrue(traversalTask.isDone());
    traversalTask.join();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue) Test(org.testng.annotations.Test)

Example 52 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project scheduling by ow2-proactive.

the class PAExecutors method newCachedBoundedThreadPool.

/**
 * Similar to Executors.newCachedThreadPool but allows a maximum pool size.
 * Cached thread pool are interesting in the sense that they can grow and shrink at will.
 * Default cachedThreadPool implementation does not allow to have a maximum capacity
 * @param corePoolSize the number of threads to keep in the pool, even if they are idle
 * @param maximumPoolSize the maximum number of threads to allow in the pool
 * @param keepAliveTime when the number of threads is greater than the core, this is the maximum time that excess idle threads
 *        will wait for new tasks before terminating.
 * @param timeUnit the time unit for the {@code keepAliveTime} argument
 * @param threadFactory the factory to use when the executor
 *        creates a new thread
 * @return the newly created thread pool
 */
public static ExecutorService newCachedBoundedThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit timeUnit, ThreadFactory threadFactory) {
    BlockingQueue<Runnable> queue = new LinkedTransferQueue<Runnable>() {

        @Override
        public boolean offer(Runnable e) {
            return tryTransfer(e);
        }
    };
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, queue, threadFactory);
    threadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                executor.getQueue().put(r);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    });
    return threadPool;
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 53 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testRemoveAll.

/**
 * removeAll(c) removes only those elements of c and reports true
 * if changed
 */
public void testRemoveAll() {
    for (int i = 1; i < SIZE; ++i) {
        LinkedTransferQueue q = populatedQueue(SIZE);
        LinkedTransferQueue p = populatedQueue(i);
        assertTrue(q.removeAll(p));
        assertEquals(SIZE - i, q.size());
        for (int j = 0; j < i; ++j) {
            assertFalse(q.contains(p.remove()));
        }
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 54 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testTransfer1.

/**
 * transfer(null) throws NullPointerException
 */
public void testTransfer1() throws InterruptedException {
    try {
        LinkedTransferQueue q = new LinkedTransferQueue();
        q.transfer(null);
        shouldThrow();
    } catch (NullPointerException success) {
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 55 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testTryTransfer6.

/**
 * tryTransfer gives up after the timeout and returns false
 */
public void testTryTransfer6() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            long startTime = System.nanoTime();
            assertFalse(q.tryTransfer(new Object(), timeoutMillis(), MILLISECONDS));
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
            checkEmpty(q);
        }
    });
    awaitTermination(t);
    checkEmpty(q);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Aggregations

LinkedTransferQueue (java.util.concurrent.LinkedTransferQueue)66 ArrayList (java.util.ArrayList)10 Iterator (java.util.Iterator)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 List (java.util.List)4 ExecutorService (java.util.concurrent.ExecutorService)4 Callable (java.util.concurrent.Callable)2 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 AbstractList (java.util.AbstractList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Queue (java.util.Queue)1 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TransferQueue (java.util.concurrent.TransferQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1