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


Java DelayQueue.put方法代碼示例

本文整理匯總了Java中java.util.concurrent.DelayQueue.put方法的典型用法代碼示例。如果您正苦於以下問題:Java DelayQueue.put方法的具體用法?Java DelayQueue.put怎麽用?Java DelayQueue.put使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.DelayQueue的用法示例。


在下文中一共展示了DelayQueue.put方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: runAndReschedule

import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
 * 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;
    }
  }
}
 
開發者ID:Netflix,項目名稱:spectator,代碼行數:35,代碼來源:Scheduler.java

示例2: main

import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
	// TODO Auto-generated method stub
	int studentNumber = 20;
	DelayQueue<Student> students = new DelayQueue<Student>();
	Random random = new Random();
	for (int i = 0; i < studentNumber; i++) {
		students.put(new Student("student" + (i + 1), 30 + random.nextInt(120)));
	}
	students.put(new Student("student",120));
	Thread teacherThread = new Thread(new Teacher(students));
	teacherThread.start();
}
 
開發者ID:leon66666,項目名稱:JavaCommon,代碼行數:13,代碼來源:Exam.java

示例3: testPut

import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
 * 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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:DelayQueueTest.java

示例4: testDrainToWithActivePut

import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
 * drainTo empties queue
 */
public void testDrainToWithActivePut() throws InterruptedException {
    final DelayQueue q = populatedQueue(SIZE);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            q.put(new PDelay(SIZE + 1));
        }});

    t.start();
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertTrue(l.size() >= SIZE);
    t.join();
    assertTrue(q.size() + l.size() >= SIZE);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:DelayQueueTest.java

示例5: 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

示例6: 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

示例7: main

import java.util.concurrent.DelayQueue; //導入方法依賴的package包/類
/**
 * 入口
 */
public static void main(String[] args) {
	Random rd = new Random(System.currentTimeMillis());
	ExecutorService es = Executors.newCachedThreadPool();

	// 初始化延遲隊列
	DelayQueue<DelayedTask> queue = new DelayQueue<DelayedTask>();
	for (int i = 0; i < 20; i++) {
		queue.add(new DelayedTask(rd.nextInt(5000)));
	}
	queue.put(new EndSentinel(5000, es));

	es.execute(new DelayedTaskConsumer(queue));
}
 
開發者ID:bdceo,項目名稱:bd-codes,代碼行數:17,代碼來源:DelayQueueDemo.java

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