當前位置: 首頁>>代碼示例>>Java>>正文


Java PriorityBlockingQueue.poll方法代碼示例

本文整理匯總了Java中java.util.concurrent.PriorityBlockingQueue.poll方法的典型用法代碼示例。如果您正苦於以下問題:Java PriorityBlockingQueue.poll方法的具體用法?Java PriorityBlockingQueue.poll怎麽用?Java PriorityBlockingQueue.poll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.PriorityBlockingQueue的用法示例。


在下文中一共展示了PriorityBlockingQueue.poll方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testPriorityQueue

import java.util.concurrent.PriorityBlockingQueue; //導入方法依賴的package包/類
public void testPriorityQueue() throws Exception {
    PriorityBlockingQueue<Priority> queue = new PriorityBlockingQueue<>();
    List<Priority> priorities = Arrays.asList(Priority.values());
    Collections.shuffle(priorities, random());

    for (Priority priority : priorities) {
        queue.add(priority);
    }

    Priority prevPriority = null;
    while (!queue.isEmpty()) {
        if (prevPriority == null) {
            prevPriority = queue.poll();
        } else {
            assertThat(queue.poll().after(prevPriority), is(true));
        }
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:19,代碼來源:PrioritizedExecutorsTests.java

示例2: testDrainToN

import java.util.concurrent.PriorityBlockingQueue; //導入方法依賴的package包/類
/**
 * drainTo(c, n) empties first min(n, size) elements of queue into c
 */
public void testDrainToN() {
    PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE * 2);
    for (int i = 0; i < SIZE + 2; ++i) {
        for (int j = 0; j < SIZE; j++)
            assertTrue(q.offer(new Integer(j)));
        ArrayList l = new ArrayList();
        q.drainTo(l, i);
        int k = (i < SIZE) ? i : SIZE;
        assertEquals(k, l.size());
        assertEquals(SIZE - k, q.size());
        for (int j = 0; j < k; ++j)
            assertEquals(l.get(j), new Integer(j));
        do {} while (q.poll() != null);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:PriorityBlockingQueueTest.java

示例3: logSensorDataOnChange

import java.util.concurrent.PriorityBlockingQueue; //導入方法依賴的package包/類
/**
 * Store sensor data so that it can be published in the next publishing cycle. Unlike
 * the other log methods, this method saves the {@link #BUFFER_SIZE_FOR_ONCHANGE_SENSORS} most
 * recent sensor readings per sensor type.
 * @param data
 */
public void logSensorDataOnChange(SensorData data) {
    PriorityBlockingQueue<SensorData> newQueue =
            new PriorityBlockingQueue<SensorData>(BUFFER_SIZE_FOR_ONCHANGE_SENSORS,
                    new Comparator<SensorData>() {
        @Override
        public int compare(SensorData o1, SensorData o2) {
            return Long.compare(o1.getTimestamp(), o2.getTimestamp());
        }
    });
    PriorityBlockingQueue<SensorData> lastData = mOnChangeData.putIfAbsent(
            data.getSensorName(), newQueue);

    if (lastData == null) {
        lastData = newQueue;
    }

    // remove old entries if necessary
    while (lastData.size() >= BUFFER_SIZE_FOR_ONCHANGE_SENSORS) {
       lastData.poll();
    }

    lastData.offer(data);
}
 
開發者ID:androidthings,項目名稱:sensorhub-cloud-iot,代碼行數:30,代碼來源:CloudPublisherService.java

示例4: testContains

import java.util.concurrent.PriorityBlockingQueue; //導入方法依賴的package包/類
/**
 * contains(x) reports true when elements added but not yet removed
 */
public void testContains() {
    PriorityBlockingQueue q = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.contains(new Integer(i)));
        q.poll();
        assertFalse(q.contains(new Integer(i)));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:PriorityBlockingQueueTest.java

示例5: main

import java.util.concurrent.PriorityBlockingQueue; //導入方法依賴的package包/類
/**
 * @param args
 */
public static void main(String[] args) {
	/*
	 * Priority queue to store the events
	 */
	PriorityBlockingQueue<Event> queue = new PriorityBlockingQueue<>();

	/*
	 * An array to store the five Thread objects
	 */
	Thread taskThreads[] = new Thread[5];

	/*
	 * Create the five threads to execute five tasks
	 */
	for (int i = 0; i < taskThreads.length; i++) {
		Task task = new Task(i, queue);
		taskThreads[i] = new Thread(task);
	}

	/*
	 * Start the five threads
	 */
	for (int i = 0; i < taskThreads.length; i++) {
		taskThreads[i].start();
	}

	/*
	 * Wait for the finalization of the five threads
	 */
	for (int i = 0; i < taskThreads.length; i++) {
		try {
			taskThreads[i].join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/*
	 * Write the events in the console
	 */
	System.out.printf("Main: Queue Size: %d\n", queue.size());
	for (int i = 0; i < taskThreads.length * 1000; i++) {
		Event event = queue.poll();
		System.out.printf("Thread %s: Priority %d\n", event.getThread(), event.getPriority());
	}
	System.out.printf("Main: Queue Size: %d\n", queue.size());
	System.out.printf("Main: End of the program\n");
}
 
開發者ID:PacktPublishing,項目名稱:Java-9-Concurrency-Cookbook-Second-Edition,代碼行數:52,代碼來源:Main.java


注:本文中的java.util.concurrent.PriorityBlockingQueue.poll方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。