Пример #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));
 }
Пример #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");
 }
 public void run() {
   try {
     while (!Thread.interrupted()) {
       Event event = q.take();
       print(event);
       event.run();
     }
   } catch (InterruptedException e) {
     // Acceptable way to exit
   }
   print("Finished Controller");
 }
 public void addEvent(Event c) {
   c.start();
   q.put(c);
 }