本文整理匯總了Java中java.util.concurrent.DelayQueue.offer方法的典型用法代碼示例。如果您正苦於以下問題:Java DelayQueue.offer方法的具體用法?Java DelayQueue.offer怎麽用?Java DelayQueue.offer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.DelayQueue
的用法示例。
在下文中一共展示了DelayQueue.offer方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test_DelayQueue_past
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
@Test
public void test_DelayQueue_past(){
DelayQueue<ScheduledDataMetadata> queue = new DelayQueue<>();
ScheduledDataMetadata scheduledDataMetadata = createScheduledDataMetadata(LocalDateTime.of(2000,1,1,1,1));
queue.offer(scheduledDataMetadata);
Assert.assertEquals(1,queue.size());
Assert.assertNotNull(queue.poll());
}
示例2: test_DelayQueue_past_order
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
@Test
public void test_DelayQueue_past_order(){
DelayQueue<ScheduledDataMetadata> queue = new DelayQueue<>();
ScheduledDataMetadata scheduledDataMetadata1 = createScheduledDataMetadata(LocalDateTime.of(2000,1,1,1,1));
queue.offer(scheduledDataMetadata1);
ScheduledDataMetadata scheduledDataMetadata2 = createScheduledDataMetadata(LocalDateTime.of(2000,1,1,1,2));
queue.offer(scheduledDataMetadata2);
Assert.assertEquals(2,queue.size());
Assert.assertEquals(scheduledDataMetadata1,queue.poll());
Assert.assertEquals(scheduledDataMetadata2,queue.poll());
}
示例3: test_DelayQueue_future
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
@Test
public void test_DelayQueue_future(){
DelayQueue<ScheduledDataMetadata> queue = new DelayQueue<>();
ScheduledDataMetadata scheduledDataMetadata = createScheduledDataMetadata(LocalDateTime.of(2100,1,1,1,1));
queue.offer(scheduledDataMetadata);
Assert.assertEquals(1,queue.size());
Assert.assertNull(queue.poll());
}
示例4: main
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public static void main(String[] args) {
// 創建延時隊列
DelayQueue<Message> queue = new DelayQueue<Message>();
// 添加延時消息,m1 延時3s
Message m1 = new Message(1, "world", 3000);
// 添加延時消息,m2 延時10s
Message m2 = new Message(2, "hello", 1000);
//將延時消息放到延時隊列中
queue.offer(m2);
queue.offer(m1);
// 啟動消費線程 消費添加到延時隊列中的消息,前提是任務到了延期時間
ExecutorService exec = Executors.newFixedThreadPool(1);
exec.execute(new Consumer(queue));
exec.shutdown();
}
示例5: replace
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* Replace the given activity in the given queue. If not in the queue, adds it to the queue. The activity
* runs after the specified delay (the delay of the previous entry, if any, is ignored)
*
* @param group the queue - all activities within a queue are executed serially
* @param activity the activity
* @param delay the delay
* @param unit the delay unit
*/
public synchronized void replace(QueueGroups group, Activity activity, long delay, TimeUnit unit)
{
ActivityHolder holder = new ActivityHolder(activity, TimeUnit.MILLISECONDS.convert(delay, unit));
DelayQueue<ActivityHolder> queue = queues.get(group);
queue.remove(holder);
queue.offer(holder);
}