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


Java DelayQueue.take方法代碼示例

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

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

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

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

示例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();
    }
}
 
開發者ID:home-climate-control,項目名稱:dz,代碼行數:44,代碼來源:DelayTest.java


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