Exemplo n.º 1
0
 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));
 }
Exemplo n.º 2
0
 public void run() {
   try {
     while (!Thread.interrupted()) q.take().run(); // Run task with the current thread
   } catch (InterruptedException e) {
     // Acceptable way to exit
   }
   print("Finished DelayedTaskConsumer");
 }