use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testTryTransfer7.
/**
* tryTransfer waits for any elements previously in to be removed
* before transfering to a poll or take
*/
public void testTryTransfer7() throws InterruptedException {
final LinkedTransferQueue q = new LinkedTransferQueue();
assertTrue(q.offer(four));
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
checkEmpty(q);
}
});
while (q.size() != 2) Thread.yield();
assertEquals(2, q.size());
assertSame(four, q.poll());
assertSame(five, q.poll());
checkEmpty(q);
awaitTermination(t);
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testPollInExecutor.
/**
* timed poll retrieves elements across Executor threads
*/
public void testPollInExecutor() {
final LinkedTransferQueue q = new LinkedTransferQueue();
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
final ExecutorService executor = Executors.newFixedThreadPool(2);
try (PoolCleaner cleaner = cleaner(executor)) {
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertNull(q.poll());
threadsStarted.await();
long startTime = System.nanoTime();
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
checkEmpty(q);
}
});
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.await();
q.put(one);
}
});
}
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testAddAllSelf.
/**
* addAll(this) throws IllegalArgumentException
*/
public void testAddAllSelf() {
LinkedTransferQueue q = populatedQueue(SIZE);
try {
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testDrainToWithActivePut.
/**
* drainTo(c) empties full queue, unblocking a waiting put.
*/
public void testDrainToWithActivePut() throws InterruptedException {
final LinkedTransferQueue q = populatedQueue(SIZE);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
q.put(SIZE + 1);
}
});
ArrayList l = new ArrayList();
q.drainTo(l);
assertTrue(l.size() >= SIZE);
for (int i = 0; i < SIZE; ++i) assertEquals(i, l.get(i));
awaitTermination(t);
assertTrue(q.size() + l.size() >= SIZE);
}
use of java.util.concurrent.LinkedTransferQueue in project bboxdb by jnidzwetzki.
the class RTreeMMFReader method getEntriesForRegion.
@Override
public synchronized List<SpatialIndexEntry> getEntriesForRegion(final BoundingBox boundingBox) throws StorageManagerException {
final List<SpatialIndexEntry> resultList = new ArrayList<>();
final Queue<Integer> readTasks = new LinkedTransferQueue<>();
readTasks.add(firstNodePos);
try {
while (!readTasks.isEmpty()) {
final int position = readTasks.remove();
memory.position(position);
final DirectoryNode directoryNode = new DirectoryNode();
directoryNode.initFromByteBuffer(memory, maxNodeSize);
if (directoryNode.getBoundingBox().overlaps(boundingBox)) {
readTasks.addAll(directoryNode.getChildNodes());
final List<SpatialIndexEntry> foundEntries = directoryNode.getIndexEntries().stream().filter(e -> e.getBoundingBox().overlaps(boundingBox)).collect(Collectors.toList());
resultList.addAll(foundEntries);
}
}
return resultList;
} catch (IOException e) {
throw new StorageManagerException(e);
}
}
Aggregations