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();
}
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;
}
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()));
}
}
}
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) {
}
}
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);
}
Aggregations