code: public void cleanupTerminatedInstances(final long period) {
  
          if (null == cleanupTerminatedInstancesTimer) {
              // "double checked locking" for the cleanup timer
              synchronized (this) {
                  if (null == cleanupTerminatedInstancesTimer) {
  
                      Runnable cleanupTerminatedInstancesTask = new Runnable() {
  
                          /**
                           * this method is triggered every pre-defined period
                           */
                          private String terminatedState = AbstractMockEc2Instance.InstanceState.TERMINATED
                                  .getName();
  
  
                          @Override
                          public void run() {
                              // traverse the map allMockEc2Instances and clean up the terminated ones
                              for (AbstractMockEc2Instance instance : allMockEc2Instances.values()) {
                                  if (terminatedState.equals(instance.getInstanceState().getName())) {
                                      allMockEc2Instances.remove(instance.getInstanceID());
                                      instance.destroyInternalTimer();
                                  }
                              }
                          }
  
                      };
  
                      cleanupTerminatedInstancesTimer = Executors.newSingleThreadScheduledExecutor();
                      cleanupTerminatedInstancesScheduledFuture = cleanupTerminatedInstancesTimer
                              .scheduleAtFixedRate(cleanupTerminatedInstancesTask, 0L, period, TimeUnit.SECONDS);
                  }
              }
          }
      }
docs: Clean up terminated mock instances after a pre-defined period. Period is defined in aws-mock.properties (or if
  not overridden, as defined in aws-mock-default.properties)
  
  @param period
  time period to clean up terminated instances