Esempio n. 1
0
public class ActiveObjectDemo {
  private ExecutorService ex = Executors.newSingleThreadExecutor();
  private Random rand = new Random(47);
  // Insert a random delay to produce the effect
  // of a calculation time:
  private void pause(int factor) {
    try {
      TimeUnit.MILLISECONDS.sleep(100 + rand.nextInt(factor));
    } catch (InterruptedException e) {
      print("sleep() interrupted");
    }
  }

  public Future<Integer> calculateInt(final int x, final int y) {
    return ex.submit(
        new Callable<Integer>() {
          public Integer call() {
            print("starting " + x + " + " + y);
            pause(500);
            return x + y;
          }
        });
  }

  public Future<Float> calculateFloat(final float x, final float y) {
    return ex.submit(
        new Callable<Float>() {
          public Float call() {
            print("starting " + x + " + " + y);
            pause(2000);
            return x + y;
          }
        });
  }

  public void shutdown() {
    ex.shutdown();
  }

  public static void main(String[] args) {
    ActiveObjectDemo d1 = new ActiveObjectDemo();
    // Prevents ConcurrentModificationException:
    List<Future<?>> results = new CopyOnWriteArrayList<Future<?>>();
    for (float f = 0.0f; f < 1.0f; f += 0.2f) results.add(d1.calculateFloat(f, f));
    for (int i = 0; i < 5; i++) results.add(d1.calculateInt(i, i));
    print("All asynch calls made");
    while (results.size() > 0) {
      for (Future<?> f : results)
        if (f.isDone()) {
          try {
            print(f.get());
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
          results.remove(f);
        }
    }
    d1.shutdown();
  }
} /* Output: (85% match)
 public static void main(String[] args) throws Exception {
   Car car = new Car();
   ExecutorService exec = Executors.newCachedThreadPool();
   exec.execute(new WaxOff(car));
   exec.execute(new WaxOn(car));
   TimeUnit.SECONDS.sleep(5); // Run for a while...
   exec.shutdownNow(); // Interrupt all tasks
 }
 public static void main(String[] args) throws Exception {
   ToastQueue dryQueue = new ToastQueue(),
       butteredQueue = new ToastQueue(),
       finishedQueue = new ToastQueue();
   ExecutorService exec = Executors.newCachedThreadPool();
   exec.execute(new Toaster(dryQueue));
   exec.execute(new Butterer(dryQueue, butteredQueue));
   exec.execute(new Jammer(butteredQueue, finishedQueue));
   exec.execute(new Eater(finishedQueue));
   TimeUnit.SECONDS.sleep(5);
   exec.shutdownNow();
 }
 public static void main(String[] args) {
   ExecutorService exec = Executors.newCachedThreadPool();
   DelayQueue<Event> queue = new DelayQueue<Event>();
   GreenhouseControls gc = new GreenhouseControls(queue);
   gc.addEvent(gc.new Bell(900));
   Event[] eventList = {
     gc.new ThermostatNight(0),
     gc.new LightOn(200),
     gc.new LightOff(400),
     gc.new WaterOn(600),
     gc.new WaterOff(800),
     gc.new ThermostatDay(1400)
   };
   gc.addEvent(gc.new Restart(2000, eventList));
   if (args.length == 1) gc.addEvent(new GreenhouseControls.Terminate(new Integer(args[0]), exec));
   exec.execute(gc);
 }
 public static void main(String[] args) throws Exception {
   ExecutorService exec = Executors.newCachedThreadPool(new DaemonThreadFactory());
   for (int i = 0; i < 10; i++) exec.execute(new DaemonFromFactory());
   print("All daemons started");
   TimeUnit.MILLISECONDS.sleep(500); // Run for a while
 }