本文整理匯總了Java中java.util.concurrent.DelayQueue.add方法的典型用法代碼示例。如果您正苦於以下問題:Java DelayQueue.add方法的具體用法?Java DelayQueue.add怎麽用?Java DelayQueue.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.DelayQueue
的用法示例。
在下文中一共展示了DelayQueue.add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public static void main(String[] args) throws Throwable {
final DelayQueue<Delayed> q = new DelayQueue<>();
final long t0 = System.nanoTime();
for (long i = 0; i < 1000; i++) {
final long expiry = t0 + i*10L*1000L*1000L;
q.add(new Delayed() {
public long getDelay(TimeUnit unit) {
return unit.convert(expiry - System.nanoTime(),
NANOSECONDS);
}
public int compareTo(Delayed x) {
long d = getDelay(NANOSECONDS)
- x.getDelay(NANOSECONDS);
return d < 0 ? -1 : d > 0 ? 1 : 0; }});
}
for (int i = 0; i < 300; i++)
new Thread() { public void run() {
try {
while (!q.isEmpty())
q.poll(10L, TimeUnit.SECONDS);
} catch (Throwable t) { t.printStackTrace(); }
}}.start();
}
示例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<>();
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: testDrainTo
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* drainTo(c) empties queue into another collection c
*/
public void testDrainTo() {
DelayQueue q = new DelayQueue();
PDelay[] elems = new PDelay[SIZE];
for (int i = 0; i < SIZE; ++i) {
elems[i] = new PDelay(i);
q.add(elems[i]);
}
ArrayList l = new ArrayList();
q.drainTo(l);
assertEquals(0, q.size());
for (int i = 0; i < SIZE; ++i)
assertEquals(elems[i], l.get(i));
q.add(elems[0]);
q.add(elems[1]);
assertFalse(q.isEmpty());
assertTrue(q.contains(elems[0]));
assertTrue(q.contains(elems[1]));
l.clear();
q.drainTo(l);
assertEquals(0, q.size());
assertEquals(2, l.size());
for (int i = 0; i < 2; ++i)
assertEquals(elems[i], l.get(i));
}
示例4: main
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public static void main(String[] args) throws Throwable {
final DelayQueue<Delayed> q = new DelayQueue<Delayed>();
final long t0 = System.nanoTime();
for (long i = 0; i < 1000; i++) {
final long expiry = t0 + i*10L*1000L*1000L;
q.add(new Delayed() {
public long getDelay(TimeUnit unit) {
return unit.convert(expiry - System.nanoTime(),
NANOSECONDS);
}
public int compareTo(Delayed x) {
long d = getDelay(NANOSECONDS)
- x.getDelay(NANOSECONDS);
return d < 0 ? -1 : d > 0 ? 1 : 0; }});
}
for (int i = 0; i < 300; i++)
new Thread() { public void run() {
try {
while (!q.isEmpty())
q.poll(10L, TimeUnit.SECONDS);
} catch (Throwable t) { t.printStackTrace(); }
}}.start();
}
示例5: 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());
}
示例6: realMain
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
private static void realMain(String[] args) throws Throwable {
DelayQueue<Godot> q = new DelayQueue<>();
for (int i = 0; i < 3; i++) {
equal(q.size(), i);
equal(q.poll(), null);
equal(q.size(), i);
equal(q.poll(100, TimeUnit.MILLISECONDS), null);
equal(q.size(), i);
q.add(new Godot());
}
}
示例7: testEmpty
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* isEmpty is true before add, false after
*/
public void testEmpty() {
DelayQueue q = new DelayQueue();
assertTrue(q.isEmpty());
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
q.add(new PDelay(1));
assertFalse(q.isEmpty());
q.add(new PDelay(2));
q.remove();
q.remove();
assertTrue(q.isEmpty());
}
示例8: testClear
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* clear removes all elements
*/
public void testClear() {
DelayQueue q = populatedQueue(SIZE);
q.clear();
assertTrue(q.isEmpty());
assertEquals(0, q.size());
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
PDelay x = new PDelay(1);
q.add(x);
assertFalse(q.isEmpty());
assertTrue(q.contains(x));
q.clear();
assertTrue(q.isEmpty());
}
示例9: testContainsAll
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* containsAll(c) is true when c contains a subset of elements
*/
public void testContainsAll() {
DelayQueue q = populatedQueue(SIZE);
DelayQueue p = new DelayQueue();
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.containsAll(p));
assertFalse(p.containsAll(q));
p.add(new PDelay(i));
}
assertTrue(p.containsAll(q));
}
示例10: testIteratorRemove
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* iterator.remove removes current element
*/
public void testIteratorRemove() {
final DelayQueue q = new DelayQueue();
q.add(new PDelay(2));
q.add(new PDelay(1));
q.add(new PDelay(3));
Iterator it = q.iterator();
it.next();
it.remove();
it = q.iterator();
assertEquals(new PDelay(2), it.next());
assertEquals(new PDelay(3), it.next());
assertFalse(it.hasNext());
}
示例11: testTimedPollDelayed
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
* timed poll of a non-empty queue returns null if no expired elements.
*/
public void testTimedPollDelayed() throws InterruptedException {
DelayQueue q = new DelayQueue();
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
long startTime = System.nanoTime();
assertNull(q.poll(timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
}
示例12: main
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public static void main(String[] args)
{
Random rand = new Random(47);
ExecutorService exec = Executors.newCachedThreadPool();
DelayQueue<DelayedTask> queue = new DelayQueue<DelayedTask>();
// Fill with tasks that have random delays:
for (int i = 0; i < 20; i++)
{
queue.put(new DelayedTask(rand.nextInt(5000)));
}
// Set the stopping point
queue.add(new DelayedTask.EndSentinel(5000, exec));
exec.execute(new DelayedTaskConsumer(queue));
}
示例13: realMain
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
private static void realMain(String[] args) throws Throwable {
DelayQueue<Godot> q = new DelayQueue<Godot>();
for (int i = 0; i < 3; i++) {
equal(q.size(), i);
equal(q.poll(), null);
equal(q.size(), i);
equal(q.poll(100, TimeUnit.MILLISECONDS), null);
equal(q.size(), i);
q.add(new Godot());
}
}
示例14: asDelayQueue
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
@SafeVarargs
public static <T extends Delayed> DelayQueue<T> asDelayQueue(final T... a) {
if (N.isNullOrEmpty(a)) {
return new DelayQueue<>();
}
final DelayQueue<T> queue = new DelayQueue<>();
for (T e : a) {
queue.add(e);
}
return queue;
}
示例15: main
import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public static void main(String[] args) {
Random rand = new Random(47);
ExecutorService exec = Executors.newCachedThreadPool();
DelayQueue<DelayedTask> queue = new DelayQueue<DelayedTask>();
// Fill with tasks that have random delays:
for (int i = 0; i < 20; i++)
queue.put(new DelayedTask(rand.nextInt(5000)));
// Set the stopping point
queue.add(new DelayedTask.EndSentinel(5000, exec));
exec.execute(new DelayedTaskConsumer(queue));
}