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


Java DelayQueue.add方法代碼示例

本文整理匯總了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();
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:26,代碼來源:Stress.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:DelayQueueTest.java

示例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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:28,代碼來源:DelayQueueTest.java

示例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();
    }
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:26,代碼來源:Stress.java

示例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());
}
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:20,代碼來源:DelayQueueTest.java

示例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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:PollUnexpired.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:DelayQueueTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:DelayQueueTest.java

示例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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:DelayQueueTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:DelayQueueTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:DelayQueueTest.java

示例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));
}
 
開發者ID:baohongfei,項目名稱:think-in-java,代碼行數:15,代碼來源:DelayQueueDemo.java

示例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());
    }
}
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:12,代碼來源:PollUnexpired.java

示例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;
}
 
開發者ID:landawn,項目名稱:AbacusUtil,代碼行數:15,代碼來源:N.java

示例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));
}
 
開發者ID:cowthan,項目名稱:JavaAyo,代碼行數:12,代碼來源:DelayQueueDemo.java


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