java.util.concurrent.DelayQueue Java Examples

The following examples show how to use java.util.concurrent.DelayQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RetryThread.java    From opensharding-spi-impl with Apache License 2.0 6 votes vote down vote up
public RetryThread(final DelayQueue<BaseOperation> queue) {
    this.queue = queue;
    retryExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), new ThreadFactory() {
        
        private final AtomicInteger threadIndex = new AtomicInteger(0);
        
        @Override
        public Thread newThread(final Runnable runnable) {
            Thread thread = new Thread(runnable);
            thread.setDaemon(true);
            thread.setName("zk-retry-" + threadIndex.incrementAndGet());
            return thread;
        }
    });
    addDelayedShutdownHook(retryExecutor, closeDelay, TimeUnit.SECONDS);
}
 
Example #2
Source File: Cpt8_TimeoutManager.java    From Zebra with MIT License 6 votes vote down vote up
/**
 * 模拟在清理session池的时候,session因重新调用而导致清理时间延迟
 * @param list
 */
public static void updateObject(final List<Session> list,final DelayQueue<Session> queue){
	Thread thread=new Thread(){
		public void run(){
			try {
				//对于iteratorDelayQueue可能存在同步的问题,但是这里因sleep时间点的问题,不会发生异常
				//暂时不需要处理
				Thread.sleep(1000);	//睡眠1000ms
				//list(4)默认生命周期是2000ms,睡眠1000后,现在应该还有1000ms左右
				list.get(4).updateTriger();
				result.add("id:"+list.get(4).id+" 寿命延长\t currentTime:"+System.currentTimeMillis());
				Thread.sleep(1000);	//再次睡眠1000ms
				//再次延长list(4),这次延时后list(4)的总生命周期应该是4000ms
				list.get(4).updateTriger();
				result.add("id:"+list.get(4).id+" 寿命延长\t currentTime:"+System.currentTimeMillis());
				//执行到此处时,一共睡眠了2000ms,list(1)的初始生命是6000ms,此时延迟应该总共生命周期为8000ms
				list.get(1).updateTriger();
				result.add("id:"+list.get(2).id+" 寿命延长\t currentTime:"+System.currentTimeMillis());
			} catch (InterruptedException e) { }
		}
	};
	thread.start();
}
 
Example #3
Source File: DefaultDelayingQueue.java    From java with Apache License 2.0 6 votes vote down vote up
private void insert(
    DelayQueue<WaitForEntry<T>> q, Map<T, WaitForEntry<T>> knownEntries, WaitForEntry entry) {
  WaitForEntry existing = knownEntries.get((T) entry.data);
  if (existing != null) {
    if (Duration.between(existing.readyAtMillis, entry.readyAtMillis).isNegative()) {
      q.remove(existing);
      existing.readyAtMillis = entry.readyAtMillis;
      q.add(existing);
    }

    return;
  }

  q.offer(entry);
  knownEntries.put((T) entry.data, entry);
}
 
Example #4
Source File: DelayQueueIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDelayQueue_whenProduceElement_thenShouldConsumeAfterGivenDelay() throws InterruptedException {
    //given
    ExecutorService executor = Executors.newFixedThreadPool(2);
    BlockingQueue<DelayObject> queue = new DelayQueue<>();
    int numberOfElementsToProduce = 2;
    int delayOfEachProducedMessageMilliseconds = 500;
    DelayQueueConsumer consumer = new DelayQueueConsumer(queue, numberOfElementsToProduce);
    DelayQueueProducer producer
      = new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);

    //when
    executor.submit(producer);
    executor.submit(consumer);

    //then
    executor.awaitTermination(5, TimeUnit.SECONDS);
    executor.shutdown();
    assertEquals(consumer.numberOfConsumedElements.get(), numberOfElementsToProduce);

}
 
Example #5
Source File: UGICache.java    From pxf with Apache License 2.0 6 votes vote down vote up
/**
 * Decrement reference count for the given session's UGI. Resets the time at which the UGI will
 * expire to UGI_CACHE_EXPIRY milliseconds in the future.
 *
 * @param session                  the session for which we want to release the UGI.
 * @param cleanImmediatelyIfNoRefs if true, destroys the UGI for the given session (only if it
 *                                 is now unreferenced).
 */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public void release(SessionId session, boolean cleanImmediatelyIfNoRefs) {

    Entry entry = cache.get(session);

    if (entry == null) {
        throw new IllegalStateException("Cannot release UGI for this session; it is not cached: " + session);
    }

    DelayQueue<Entry> expirationQueue = getExpirationQueue(session.getSegmentId());

    synchronized (expirationQueue) {
        entry.decrementRefCount();
        expirationQueue.remove(entry);
        if (cleanImmediatelyIfNoRefs && entry.isNotInUse()) {
            closeUGI(entry);
        } else {
            // Reset expiration time and put it back in the queue
            // only when we don't close the UGI
            entry.resetTime();
            expirationQueue.offer(entry);
        }
    }
}
 
Example #6
Source File: Iterate.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    Godot[] godots = new Godot[] { new Godot(), new Godot(), new Godot() };
    DelayQueue<Godot> q = new DelayQueue<>(Arrays.asList(godots));
    Iterator<Godot> it = q.iterator();
    q.clear();
    check(it.hasNext());
    equal(it.next(), godots[0]);
    it.remove();
    check(q.isEmpty());

    q.addAll(Arrays.asList(godots));
    it = q.iterator();
    check(it.hasNext());
    it.next();
    equal(it.next(), godots[1]);
    it.remove();
    equal(q.size(), 2);
    check(q.contains(godots[0]));
    check(q.contains(godots[2]));
}
 
Example #7
Source File: Cpt8_TimeoutManager.java    From Zebra with MIT License 6 votes vote down vote up
public static void testDelayQueue(){
	DelayQueue<Session> queue=new DelayQueue<Session>();
	Random random=new Random(47);
	StringBuilder sb=new StringBuilder();
	List<Session> list=new ArrayList<Session>();
	//生产对象添加到队列中
	for(int i=0;i<5;i++){
		long timeout=(random.nextInt(10)+1)*1000;	//11以内的整数乘以1000毫秒
		Session temp=new Session(timeout);
		sb.append("id:"+temp.id+"-").append(timeout).append(" ");
		list.add(temp);
		queue.offer(temp);
	}
	System.out.println("=========================添加到队列中的顺序=========================");
	System.out.println(sb.toString());
	//可以先观察queue的排序结果
	System.out.println("=========================队列中实际的顺序========================");
	System.out.println(iteratorDelayQueue(queue));
	System.out.println("=========================启动清理线程==============================");
	monitorThread(queue);	//启动监控清理线程
	//可先不执行延迟清理,进行观察
	updateObject(list,queue);	//模拟因session最新被调用,而延迟清理
}
 
Example #8
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * timed poll transfers elements across Executor tasks
 */
public void testPollInExecutor() {
    final DelayQueue q = new DelayQueue();
    final CheckedBarrier threadsStarted = new CheckedBarrier(2);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    try (PoolCleaner cleaner = cleaner(executor)) {
        executor.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                assertNull(q.poll());
                threadsStarted.await();
                assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
                checkEmpty(q);
            }});

        executor.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadsStarted.await();
                q.put(new PDelay(1));
            }});
    }
}
 
Example #9
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #10
Source File: DefaultGroovyMethodsSupport.java    From groovy with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected static <T> Queue<T> createSimilarQueue(Queue<T> orig) {
    if (orig instanceof ArrayBlockingQueue) {
        ArrayBlockingQueue queue = (ArrayBlockingQueue) orig;
        return new ArrayBlockingQueue<T>(queue.size() + queue.remainingCapacity());
    } else if (orig instanceof ArrayDeque) {
        return new ArrayDeque<T>();
    } else if (orig instanceof ConcurrentLinkedQueue) {
        return new ConcurrentLinkedQueue<T>();
    } else if (orig instanceof DelayQueue) {
        return new DelayQueue();
    } else if (orig instanceof LinkedBlockingDeque) {
        return new LinkedBlockingDeque<T>();
    } else if (orig instanceof LinkedBlockingQueue) {
        return new LinkedBlockingQueue<T>();
    } else if (orig instanceof PriorityBlockingQueue) {
        return new PriorityBlockingQueue<T>();
    } else if (orig instanceof PriorityQueue) {
        return new PriorityQueue<T>(11, ((PriorityQueue) orig).comparator());
    } else if (orig instanceof SynchronousQueue) {
        return new SynchronousQueue<T>();
    } else {
        return new LinkedList<T>();
    }
}
 
Example #11
Source File: CustomDelayQueue.java    From java-interview with Apache License 2.0 6 votes vote down vote up
private static void producer(DelayQueue<DelayedElement> delayQueue, String name) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                // 产生 1~5 秒的随机数
                long time = 1000L * (new Random().nextInt(5) + 1);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 组合消息体
                String message = String.format("%s,消息编号:%s 发送时间:%s 延迟:%s 秒",
                        name, MESSAGENO.getAndIncrement(), DateFormat.getDateTimeInstance().format(new Date()), time / 1000);
                // 生产消息
                delayQueue.put(new DelayedElement(message, time));
            }
        }
    }).start();
}
 
Example #12
Source File: CustomDelayQueue.java    From java-interview with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    DelayQueue<DelayedElement> delayQueue = new DelayQueue<>();
    // 生产者1
    producer(delayQueue, "生产者1");

    // 生产者2
    producer(delayQueue, "生产者2");

    // 消费者
    consumer(delayQueue);

    /* 执行结果
        生产者1,消息编号:1 发送时间:2019-6-12 20:38:37 延迟:2 秒 |执行时间:2019-6-12 20:38:39
        生产者2,消息编号:2 发送时间:2019-6-12 20:38:37 延迟:2 秒 |执行时间:2019-6-12 20:38:39
        生产者1,消息编号:3 发送时间:2019-6-12 20:38:41 延迟:4 秒 |执行时间:2019-6-12 20:38:45
        生产者1,消息编号:5 发送时间:2019-6-12 20:38:43 延迟:2 秒 |执行时间:2019-6-12 20:38:45
        ....
     */
}
 
Example #13
Source File: Scheduler.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Execute the task and if reschedule another execution.
 *
 * @param queue
 *     Queue for the pool. This task will be added to the queue to schedule
 *     future executions.
 * @param stats
 *     Handle to stats that should be updated based on the execution of the
 *     task.
 */
@SuppressWarnings("PMD.AvoidCatchingThrowable")
void runAndReschedule(DelayQueue<DelayedTask> queue, Stats stats) {
  thread = Thread.currentThread();
  boolean scheduleAgain = options.schedulingPolicy != Policy.RUN_ONCE;
  try {
    if (!isDone()) {
      task.run();
    }
  } catch (Throwable t) {
    // This catches Throwable because we cannot control the task and thus cannot
    // ensure it is well behaved with respect to exceptions.
    LOGGER.warn("task execution failed", t);
    stats.incrementUncaught(t);
    scheduleAgain = !options.stopOnFailure;
  } finally {
    thread = null;
    if (scheduleAgain && !isDone()) {
      updateNextExecutionTime(stats.skipped());
      queue.put(this);
    } else {
      cancelled = true;
    }
  }
}
 
Example #14
Source File: SystemTimer.java    From kop with Apache License 2.0 6 votes vote down vote up
private SystemTimer(String executorName,
                    long tickMs,
                    int wheelSize,
                    long startMs) {
    this.taskExecutor = Executors.newFixedThreadPool(
        1, new ThreadFactoryBuilder()
            .setDaemon(false)
            .setNameFormat("system-timer-%d")
            .build()
    );
    this.delayQueue = new DelayQueue();
    this.taskCounter = new AtomicInteger(0);
    this.timingWheel = new TimingWheel(
        tickMs,
        wheelSize,
        startMs,
        taskCounter,
        delayQueue
    );
    this.readWriteLock = new ReentrantReadWriteLock();
    this.readLock = readWriteLock.readLock();
    this.writeLock = readWriteLock.writeLock();
    this.reinsert = timerTaskEntry -> addTimerTaskEntry(timerTaskEntry);
}
 
Example #15
Source File: MemoryMailQueueFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
public MemoryCacheableMailQueue(MailQueueName name, MailQueueItemDecoratorFactory mailQueueItemDecoratorFactory) {
    this.mailItems = new DelayQueue<>();
    this.inProcessingMailItems = new LinkedBlockingDeque<>();
    this.name = name;
    this.flux = Mono.fromCallable(mailItems::take)
        .repeat()
        .subscribeOn(Schedulers.elastic())
        .flatMap(item ->
            Mono.fromRunnable(() -> inProcessingMailItems.add(item)).thenReturn(item))
        .map(item -> mailQueueItemDecoratorFactory.decorate(item, name));
}
 
Example #16
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #17
Source File: NMSimulator.java    From big-c with Apache License 2.0 5 votes vote down vote up
public void init(String nodeIdStr, int memory, int cores,
        int dispatchTime, int heartBeatInterval, ResourceManager rm)
        throws IOException, YarnException {
  super.init(dispatchTime, dispatchTime + 1000000L * heartBeatInterval,
          heartBeatInterval);
  // create resource
  String rackHostName[] = SLSUtils.getRackHostName(nodeIdStr);
  this.node = NodeInfo.newNodeInfo(rackHostName[0], rackHostName[1], 
                BuilderUtils.newResource(memory, cores));
  this.rm = rm;
  // init data structures
  completedContainerList =
          Collections.synchronizedList(new ArrayList<ContainerId>());
  releasedContainerList =
          Collections.synchronizedList(new ArrayList<ContainerId>());
  containerQueue = new DelayQueue<ContainerSimulator>();
  amContainerList =
          Collections.synchronizedList(new ArrayList<ContainerId>());
  runningContainers =
          new ConcurrentHashMap<ContainerId, ContainerSimulator>();
  // register NM with RM
  RegisterNodeManagerRequest req =
          Records.newRecord(RegisterNodeManagerRequest.class);
  req.setNodeId(node.getNodeID());
  req.setResource(node.getTotalCapability());
  req.setHttpPort(80);
  RegisterNodeManagerResponse response = rm.getResourceTrackerService()
          .registerNodeManager(req);
  masterKey = response.getNMTokenMasterKey();
}
 
Example #18
Source File: NMSimulator.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public void init(String nodeIdStr, int memory, int cores,
        int dispatchTime, int heartBeatInterval, ResourceManager rm)
        throws IOException, YarnException {
  super.init(dispatchTime, dispatchTime + 1000000L * heartBeatInterval,
          heartBeatInterval);
  // create resource
  String rackHostName[] = SLSUtils.getRackHostName(nodeIdStr);
  this.node = NodeInfo.newNodeInfo(rackHostName[0], rackHostName[1], 
                BuilderUtils.newResource(memory, cores));
  this.rm = rm;
  // init data structures
  completedContainerList =
          Collections.synchronizedList(new ArrayList<ContainerId>());
  releasedContainerList =
          Collections.synchronizedList(new ArrayList<ContainerId>());
  containerQueue = new DelayQueue<ContainerSimulator>();
  amContainerList =
          Collections.synchronizedList(new ArrayList<ContainerId>());
  runningContainers =
          new ConcurrentHashMap<ContainerId, ContainerSimulator>();
  // register NM with RM
  RegisterNodeManagerRequest req =
          Records.newRecord(RegisterNodeManagerRequest.class);
  req.setNodeId(node.getNodeID());
  req.setResource(node.getTotalCapability());
  req.setHttpPort(80);
  RegisterNodeManagerResponse response = rm.getResourceTrackerService()
          .registerNodeManager(req);
  masterKey = response.getNMTokenMasterKey();
}
 
Example #19
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * iterator iterates through all elements
 */
public void testIterator() {
    DelayQueue q = populatedQueue(SIZE);
    int i = 0;
    Iterator it = q.iterator();
    while (it.hasNext()) {
        assertTrue(q.contains(it.next()));
        ++i;
    }
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);
}
 
Example #20
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Initializing from Collection of null elements throws NPE
 */
public void testConstructor4() {
    try {
        new DelayQueue(Arrays.asList(new PDelay[SIZE]));
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example #21
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * take retrieves elements in priority order
 */
public void testTake() throws InterruptedException {
    DelayQueue q = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(new PDelay(i), q.take());
    }
}
 
Example #22
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #23
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * put doesn't block waiting for take
 */
public void testPutWithTake() throws InterruptedException {
    final DelayQueue q = new DelayQueue();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            q.put(new PDelay(0));
            q.put(new PDelay(0));
            q.put(new PDelay(0));
            q.put(new PDelay(0));
        }});

    awaitTermination(t);
    assertEquals(4, q.size());
}
 
Example #24
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * all elements successfully put are contained
 */
public void testPut() {
    DelayQueue q = new DelayQueue();
    for (int i = 0; i < SIZE; ++i) {
        PDelay x = new PDelay(i);
        q.put(x);
        assertTrue(q.contains(x));
    }
    assertEquals(SIZE, q.size());
}
 
Example #25
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Queue contains all elements of successful addAll
 */
public void testAddAll5() {
    PDelay[] empty = new PDelay[0];
    PDelay[] ints = new PDelay[SIZE];
    for (int i = SIZE - 1; i >= 0; --i)
        ints[i] = new PDelay(i);
    DelayQueue q = new DelayQueue();
    assertFalse(q.addAll(Arrays.asList(empty)));
    assertTrue(q.addAll(Arrays.asList(ints)));
    for (int i = 0; i < SIZE; ++i)
        assertEquals(ints[i], q.poll());
}
 
Example #26
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addAll of a collection with any null elements throws NPE after
 * possibly adding some elements
 */
public void testAddAll3() {
    DelayQueue q = new DelayQueue();
    PDelay[] a = new PDelay[SIZE];
    for (int i = 0; i < SIZE - 1; ++i)
        a[i] = new PDelay(i);
    try {
        q.addAll(Arrays.asList(a));
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example #27
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addAll(this) throws IAE
 */
public void testAddAllSelf() {
    DelayQueue q = populatedQueue(SIZE);
    try {
        q.addAll(q);
        shouldThrow();
    } catch (IllegalArgumentException success) {}
}
 
Example #28
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * add succeeds
 */
public void testAdd() {
    DelayQueue q = new DelayQueue();
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.size());
        assertTrue(q.add(new PDelay(i)));
    }
}
 
Example #29
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #30
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * drainTo(c, n) empties first min(n, size) elements of queue into c
 */
public void testDrainToN() {
    for (int i = 0; i < SIZE + 2; ++i) {
        DelayQueue q = populatedQueue(SIZE);
        ArrayList l = new ArrayList();
        q.drainTo(l, i);
        int k = (i < SIZE) ? i : SIZE;
        assertEquals(SIZE - k, q.size());
        assertEquals(k, l.size());
    }
}