本文整理匯總了Java中java.util.concurrent.DelayQueue.poll方法的典型用法代碼示例。如果您正苦於以下問題:Java DelayQueue.poll方法的具體用法?Java DelayQueue.poll怎麽用?Java DelayQueue.poll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.DelayQueue
的用法示例。
在下文中一共展示了DelayQueue.poll方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testElement
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* element returns next element, or throws NSEE if empty
*/
public void testElement() {
DelayQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(new PDelay(i), q.element());
q.poll();
}
try {
q.element();
shouldThrow();
} catch (NoSuchElementException success) {}
}
示例2: testContains
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* contains(x) reports true when elements added but not yet removed
*/
public void testContains() {
DelayQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.contains(new PDelay(i)));
q.poll();
assertFalse(q.contains(new PDelay(i)));
}
}
示例3: main
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* @param args
*/
public static void main(String[] args) throws Exception {
/*
* Delayed queue to store the events
*/
DelayQueue<Event> queue = new DelayQueue<>();
/*
* An array to store the Thread objects that execute the tasks
*/
Thread threads[] = new Thread[5];
/*
* Create the five tasks
*/
for (int i = 0; i < threads.length; i++) {
Task task = new Task(i + 1, queue);
threads[i] = new Thread(task);
}
/*
* Execute the five tasks
*/
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
/*
* Wait for the finalization of the five tasks
*/
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
/*
* Write the results to the console
*/
do {
int counter = 0;
Event event;
do {
event = queue.poll();
if (event != null)
counter++;
} while (event != null);
System.out.printf("At %s you have read %d events\n", new Date(), counter);
TimeUnit.MILLISECONDS.sleep(500);
} while (queue.size() > 0);
}