Java Code Examples for java.util.concurrent.LinkedBlockingQueue#iterator()

The following examples show how to use java.util.concurrent.LinkedBlockingQueue#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * iterator iterates through all elements
 */
public void testIterator() throws InterruptedException {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    Iterator it = q.iterator();
    int i;
    for (i = 0; it.hasNext(); i++)
        assertTrue(q.contains(it.next()));
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);

    it = q.iterator();
    for (i = 0; it.hasNext(); i++)
        assertEquals(it.next(), q.take());
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);
}
 
Example 2
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * iterator.remove removes current element
 */
public void testIteratorRemove() {
    final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
    q.add(two);
    q.add(one);
    q.add(three);

    Iterator it = q.iterator();
    it.next();
    it.remove();

    it = q.iterator();
    assertSame(it.next(), one);
    assertSame(it.next(), three);
    assertFalse(it.hasNext());
}
 
Example 3
Source File: LicenseSchedulingPolicy.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
private void removeFinishedJobsAndReleaseTokens(String requiredLicense) {

        LinkedBlockingQueue<String> eligibleJobsDescriptorsLicense = licenseSynchronization.getPersistedJobsLicense(requiredLicense);

        Iterator<String> iter = eligibleJobsDescriptorsLicense.iterator();
        String currentJobId;
        int currentNbTokens = licenseSynchronization.getRemainingTokens(requiredLicense);
        while (iter.hasNext()) {
            currentJobId = iter.next();

            if (!schedulingService.isJobAlive(JobIdImpl.makeJobId(currentJobId))) {
                logger.debug("Releasing token for license " + requiredLicense + " given to job " + currentJobId);
                iter.remove();
                currentNbTokens++;
            } else {
                logger.debug("Job " + currentJobId + " is still alive, cannot release its token");
            }
        }
        licenseSynchronization.setNbTokens(requiredLicense, currentNbTokens);
        licenseSynchronization.markJobLicenseChange(requiredLicense);
        licenseSynchronization.persist();
    }
 
Example 4
Source File: LicenseSchedulingPolicy.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
private void removeFinishedTasksAndReleaseTokens(String requiredLicense) {

        LinkedBlockingQueue<String> eligibleTasksDescriptorsLicense = licenseSynchronization.getPersistedTasksLicense(requiredLicense);

        Iterator<String> iter = eligibleTasksDescriptorsLicense.iterator();
        String currentTaskId;
        int currentNbTokens = licenseSynchronization.getRemainingTokens(requiredLicense);
        while (iter.hasNext()) {
            currentTaskId = iter.next();

            if (!schedulingService.isTaskAlive(TaskIdImpl.makeTaskId(currentTaskId))) {
                logger.debug("releasing token for license " + requiredLicense + " given to task " + currentTaskId);
                iter.remove();
                currentNbTokens++;
            }
        }
        licenseSynchronization.setNbTokens(requiredLicense, currentNbTokens);
        licenseSynchronization.markTaskLicenseChange(requiredLicense);
        licenseSynchronization.persist();
    }
 
Example 5
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * iterator iterates through all elements
 */
public void testIterator() throws InterruptedException {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    Iterator it = q.iterator();
    int i;
    for (i = 0; it.hasNext(); i++)
        assertTrue(q.contains(it.next()));
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);

    it = q.iterator();
    for (i = 0; it.hasNext(); i++)
        assertEquals(it.next(), q.take());
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);
}
 
Example 6
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * iterator.remove removes current element
 */
public void testIteratorRemove() {
    final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
    q.add(two);
    q.add(one);
    q.add(three);

    Iterator it = q.iterator();
    it.next();
    it.remove();

    it = q.iterator();
    assertSame(it.next(), one);
    assertSame(it.next(), three);
    assertFalse(it.hasNext());
}
 
Example 7
Source File: DependencyQueue.java    From ovsdb with Eclipse Public License 1.0 6 votes vote down vote up
private List<DependentJob> getReadyJobs(LinkedBlockingQueue<DependentJob> queue) {
    List<DependentJob> readyJobs = new ArrayList<>();
    Iterator<DependentJob> jobIterator = queue.iterator();
    while (jobIterator.hasNext()) {
        DependentJob job = jobIterator.next();
        long currentTime = System.currentTimeMillis();
        if (job.areDependenciesMet(deviceInfo)) {
            jobIterator.remove();
            readyJobs.add(job);
            continue;
        }
        if (job.isExpired(currentTime)) {
            deviceInfo.clearKeyFromDependencyQueue(job.getKey());
            jobIterator.remove();
            continue;
        }
    }
    return readyJobs;
}
 
Example 8
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * iterator ordering is FIFO
 */
public void testIteratorOrdering() {
    final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);
    assertEquals(0, q.remainingCapacity());
    int k = 0;
    for (Iterator it = q.iterator(); it.hasNext();) {
        assertEquals(++k, it.next());
    }
    assertEquals(3, k);
}
 
Example 9
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Modifications do not cause iterators to fail
 */
public void testWeaklyConsistentIteration() {
    final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);
    for (Iterator it = q.iterator(); it.hasNext();) {
        q.remove();
        it.next();
    }
    assertEquals(0, q.size());
}
 
Example 10
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * iterator ordering is FIFO
 */
public void testIteratorOrdering() {
    final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);
    assertEquals(0, q.remainingCapacity());
    int k = 0;
    for (Iterator it = q.iterator(); it.hasNext();) {
        assertEquals(++k, it.next());
    }
    assertEquals(3, k);
}
 
Example 11
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Modifications do not cause iterators to fail
 */
public void testWeaklyConsistentIteration() {
    final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);
    for (Iterator it = q.iterator(); it.hasNext();) {
        q.remove();
        it.next();
    }
    assertEquals(0, q.size());
}