本文整理匯總了Java中java.util.concurrent.DelayQueue.take方法的典型用法代碼示例。如果您正苦於以下問題:Java DelayQueue.take方法的具體用法?Java DelayQueue.take怎麽用?Java DelayQueue.take使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.DelayQueue
的用法示例。
在下文中一共展示了DelayQueue.take方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testDelay
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* Delayed actions do not occur until their delay elapses
*/
public void testDelay() throws InterruptedException {
DelayQueue<NanoDelay> q = new DelayQueue<>();
for (int i = 0; i < SIZE; ++i)
q.add(new NanoDelay(1000000L * (SIZE - i)));
long last = 0;
for (int i = 0; i < SIZE; ++i) {
NanoDelay e = q.take();
long tt = e.getTriggerTime();
assertTrue(System.nanoTime() - tt >= 0);
if (i != 0)
assertTrue(tt >= last);
last = tt;
}
assertTrue(q.isEmpty());
}
示例2: testDelay
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* Delayed actions do not occur until their delay elapses
*/
public void testDelay() throws InterruptedException {
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
for (int i = 0; i < SIZE; ++i)
q.add(new NanoDelay(1000000L * (SIZE - i)));
long last = 0;
for (int i = 0; i < SIZE; ++i) {
NanoDelay e = q.take();
long tt = e.getTriggerTime();
assertTrue(System.nanoTime() - tt >= 0);
if (i != 0)
assertTrue(tt >= last);
last = tt;
}
assertTrue(q.isEmpty());
}
示例3: start
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public void start(final Runnable task)
{
semaphore = new Semaphore(0);
shouldRun = true;
Thread thread = new Thread(new Runnable() {
@Override
public void run()
{
try{
DelayQueue<Delayed> dq = new DelayQueue<Delayed>();
MyDelayed dl = new MyDelayed();
while(shouldRun)
{
task.run();
dl.nextAt(period, TimeUnit.NANOSECONDS);
dq.add(dl);
try {
dq.take();
} catch (InterruptedException e) {
// Thread interruption is fine.
}
}
}finally{
semaphore.release();
}
}
});
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
示例4: start
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public void start(final Runnable taskA, final Runnable taskB)
{
dlA = new MyDelayed(taskA);
dlB = new MyDelayed(taskB);
semaphore = new Semaphore(0);
shouldRun = true;
Thread thread = new Thread(new Runnable() {
@Override
public void run()
{
try{
DelayQueue<MyDelayed> dq = new DelayQueue<MyDelayed>();
dlA.nextAt(period, TimeUnit.NANOSECONDS);
dq.add(dlA);
dlB.nextAt(period, TimeUnit.NANOSECONDS);
dq.add(dlB);
while(shouldRun)
{
try {
MyDelayed dl = dq.take();
dl.task.run();
dl.nextAt(period, TimeUnit.NANOSECONDS);
dq.add(dl);
} catch (InterruptedException e) {
// Thread interruption is fine.
}
}
}finally{
semaphore.release();
}
}
});
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
示例5: takeWithoutInterrupt
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* @return null (if an interrupt) or an instance of E; resets interrupt on calling thread.
*/
public static <E extends Delayed> E takeWithoutInterrupt(final DelayQueue<E> queue) {
try {
return queue.take();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
}
示例6: testDelayQueue
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public void testDelayQueue() throws InterruptedException {
NDC.push("testDelayQueue");
try {
DelayQueue<DelayedCommand> queue = new DelayQueue<DelayedCommand>();
DelayedCommand c1 = new DelayedCommand(50);
DelayedCommand c2 = new DelayedCommand(100);
DelayedCommand c3 = new DelayedCommand(150);
queue.put(c1);
logger.info("Queue: " + queue);
queue.put(c2);
logger.info("Queue: " + queue);
queue.put(c3);
logger.info("Queue: " + queue);
long start = System.currentTimeMillis();
while (!queue.isEmpty()) {
DelayedCommand c = queue.take();
logger.info("Running " + c);
c.run();
}
logger.info(c1.getStart() - start);
logger.info(c2.getStart() - c1.getStart());
logger.info(c3.getStart() - c2.getStart());
// VT: NOTE: Better, but still too clumsy without manipulations with
// shared variable state.
} finally {
NDC.pop();
}
}