Esempio n. 1
0
  static void test() throws Throwable {
    final ExecutorService executor = Executors.newCachedThreadPool();

    final NotificationReceiver notifiee1 = new NotificationReceiver();
    final NotificationReceiver notifiee2 = new NotificationReceiver();

    final Collection<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
    tasks.add(new BlockingTask(notifiee1));
    tasks.add(new BlockingTask(notifiee2));
    tasks.add(new NonBlockingTask());

    // start a thread to invoke the tasks
    Thread thread =
        new Thread() {
          public void run() {
            try {
              executor.invokeAll(tasks);
            } catch (RejectedExecutionException t) {
              /* OK */
            } catch (Throwable t) {
              unexpected(t);
            }
          }
        };
    thread.start();

    // Wait until tasks begin execution
    notifiee1.waitForNotification();
    notifiee2.waitForNotification();

    // Now try to shutdown the executor service while tasks
    // are blocked.  This should cause the tasks to be
    // interrupted.
    executor.shutdownNow();
    if (!executor.awaitTermination(5, TimeUnit.SECONDS)) throw new Error("Executor stuck");

    // Wait for the invocation thread to complete.
    thread.join(1000);
    if (thread.isAlive()) {
      thread.interrupt();
      thread.join(1000);
      throw new Error("invokeAll stuck");
    }
  }
Esempio n. 2
0
    public Object call() throws InterruptedException {
      notifiee.sendNotification();

      // wait indefinitely until task is interrupted
      while (true) {
        synchronized (this) {
          wait();
        }
      }
    }