Search in sources :

Example 16 with LinkedTransferQueue

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);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 17 with LinkedTransferQueue

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);
            }
        });
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 18 with LinkedTransferQueue

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) {
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 19 with LinkedTransferQueue

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);
}
Also used : ArrayList(java.util.ArrayList) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 20 with LinkedTransferQueue

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);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue) Logger(org.slf4j.Logger) DataEncoderHelper(org.bboxdb.commons.io.DataEncoderHelper) UnsafeMemoryHelper(org.bboxdb.commons.io.UnsafeMemoryHelper) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) BoundingBox(org.bboxdb.commons.math.BoundingBox) ArrayList(java.util.ArrayList) Const(org.bboxdb.misc.Const) AbstractRTreeReader(org.bboxdb.storage.sstable.spatialindex.rtree.AbstractRTreeReader) List(java.util.List) Queue(java.util.Queue) StorageManagerException(org.bboxdb.storage.StorageManagerException) FileChannel(java.nio.channels.FileChannel) SpatialIndexEntry(org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry) MappedByteBuffer(java.nio.MappedByteBuffer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StorageManagerException(org.bboxdb.storage.StorageManagerException) SpatialIndexEntry(org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry) 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