예제 #1
0
  public static void startSystem() throws ExecutionException, InterruptedException {

    // Create the entry gates and link them to the parking lot
    for (int i = 0; i < numOfEntries; i++) {
      entries.add(new Entry("entry" + i, parkingLot));
    }
    parkingLot.setEntries(entries);

    // Create the exit gates and link them to the parking lot
    for (int k = 0; k < numOfExits; k++) {
      exits.add(new Exit("exit" + k, parkingLot));
    }
    parkingLot.setExits(exits);

    // Create a thread pool based on the number of entries and exits for this lot
    ScheduledExecutorService scheduledExecutorService1 =
        Executors.newScheduledThreadPool(numOfEntries);
    ScheduledExecutorService scheduledExecutorService2 =
        Executors.newScheduledThreadPool(numOfExits);

    // Schedule the threads, which will act as Car objects entering and exiting the parking lot
    ScheduledFuture<?> scheduledFuture1 =
        scheduledExecutorService1.scheduleWithFixedDelay(
            new Car(entries, exits, 0), firstCarEntryTime, enteringCarsDelay, TimeUnit.SECONDS);

    ScheduledFuture<?> scheduledFuture2 =
        scheduledExecutorService2.scheduleWithFixedDelay(
            new Car(entries, exits, 1), firstCarExitTime, exitingCarsDelay, TimeUnit.SECONDS);

    scheduledFuture1.get();
    scheduledFuture2.get();
    scheduledExecutorService1.shutdown();
    scheduledExecutorService2.shutdown();
  }
 public static void main(String[] args) throws InterruptedException, ExecutionException {
   // *1
   ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
   // *2
   Runnable task1 =
       new Runnable() {
         public void run() {
           System.out.println("Taskrepeating.");
         }
       };
   // *3 每隔5秒执行一次
   final ScheduledFuture future1 = service.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);
   // *4
   ScheduledFuture future2 =
       service.schedule(
           new Callable() {
             public String call() {
               future1.cancel(true);
               return "taskcancelled!";
             }
           },
           10,
           TimeUnit.SECONDS);
   System.out.println(future2.get());
   // *5
   service.shutdown();
 }
  @Test
  public void testScheduleCallable() throws Exception {
    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isZero();
    assertThat(duration.getCount()).isZero();

    assertThat(scheduledOnce.getCount()).isZero();
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();

    final Object obj = new Object();

    ScheduledFuture<Object> theFuture =
        instrumentedScheduledExecutor.schedule(
            new Callable<Object>() {
              public Object call() {
                assertThat(submitted.getCount()).isZero();

                assertThat(running.getCount()).isEqualTo(1);
                assertThat(completed.getCount()).isZero();
                assertThat(duration.getCount()).isZero();

                assertThat(scheduledOnce.getCount()).isEqualTo(1);
                assertThat(scheduledRepetitively.getCount()).isZero();
                assertThat(scheduledOverrun.getCount()).isZero();
                assertThat(percentOfPeriod.getCount()).isZero();

                return obj;
              }
            },
            10L,
            TimeUnit.MILLISECONDS);

    assertThat(theFuture.get()).isEqualTo(obj);

    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isEqualTo(1);
    assertThat(duration.getCount()).isEqualTo(1);
    assertThat(duration.getSnapshot().size()).isEqualTo(1);

    assertThat(scheduledOnce.getCount()).isEqualTo(1);
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();
  }
  @Test
  public void testScheduleRunnable() throws Exception {
    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isZero();
    assertThat(duration.getCount()).isZero();

    assertThat(scheduledOnce.getCount()).isZero();
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();

    ScheduledFuture<?> theFuture =
        instrumentedScheduledExecutor.schedule(
            new Runnable() {
              public void run() {
                assertThat(submitted.getCount()).isZero();

                assertThat(running.getCount()).isEqualTo(1);
                assertThat(completed.getCount()).isZero();
                assertThat(duration.getCount()).isZero();

                assertThat(scheduledOnce.getCount()).isEqualTo(1);
                assertThat(scheduledRepetitively.getCount()).isZero();
                assertThat(scheduledOverrun.getCount()).isZero();
                assertThat(percentOfPeriod.getCount()).isZero();
              }
            },
            10L,
            TimeUnit.MILLISECONDS);

    theFuture.get();

    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isEqualTo(1);
    assertThat(duration.getCount()).isEqualTo(1);
    assertThat(duration.getSnapshot().size()).isEqualTo(1);

    assertThat(scheduledOnce.getCount()).isEqualTo(1);
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();
  }
  @Test
  public void testLifecyclepool() throws Exception {

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    Runnable worker = new LifecycleRunnable(scheduler, loaderApplication, "10");
    // über command line interface konfigurierbar machen
    long initialDelay = 10;
    long period = 5;
    ScheduledFuture<?> future =
        scheduler.scheduleAtFixedRate(worker, initialDelay, period, TimeUnit.SECONDS);
    // this is just a mad shutdown thread after 30 seconds - nothing real
    // condition should be: all objects are ingested
    long shutdownDelay = 30;
    Runnable stopLCCheck = new StopLifecycelTask(future, scheduler, loaderApplication);
    ScheduledFuture<?> stopFuture =
        scheduler.schedule(stopLCCheck, shutdownDelay, TimeUnit.SECONDS);
    System.out.println("stop LC task after: : " + shutdownDelay + " seconds " + stopFuture.get());
  }