Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.gh.bmd.jrt.runner;
 
 
 import java.util.Map;
 
Class implementing a runner employing an executor service.

Created by davide-maestroni on 10/14/14.

 
 class ScheduledRunner implements Runner {
 
             new WeakIdentityHashMap<ExecutionWeakHashMap<ScheduledFuture<?>, Void>>();
 
     private final ScheduledExecutorService mService;
 
     private final Map<ThreadVoidmThreads =
             Collections.synchronizedMap(new WeakIdentityHashMap<ThreadVoid>());

    
Constructor.

Parameters:
service the executor service.
 
     @SuppressWarnings("ConstantConditions")
     ScheduledRunner(@Nonnull final ScheduledExecutorService service) {
 
         if (service == null) {
 
             throw new NullPointerException("the executor service must not be null");
         }
 
          = service;
     }
 
     public void cancel(@Nonnull final Execution execution) {
 
         synchronized () {
 
             final WeakHashMap<ScheduledFuture<?>, VoidscheduledFutures =
                     .remove(execution);
 
             if (scheduledFutures != null) {
 
                 for (final ScheduledFuture<?> future : scheduledFutures.keySet()) {
 
                     future.cancel(false);
                 }
             }
         }
     }
 
     public boolean isExecutionThread() {
 
         return .containsKey(Thread.currentThread());
     }
 
     public void run(@Nonnull final Execution executionfinal long delay,
             @Nonnull final TimeUnit timeUnit) {
 
         final ScheduledFuture<?> future =
                 .schedule(new ExecutionWrapper(execution), delaytimeUnit);
 
         if (execution.mayBeCanceled()) {
 
             synchronized () {
 
                 final WeakIdentityHashMap<ExecutionWeakHashMap<ScheduledFuture<?>, Void>>
                         futures = ;
                 WeakHashMap<ScheduledFuture<?>, VoidscheduledFutures = futures.get(execution);
 
                 if (scheduledFutures == null) {
 
                     scheduledFutures = new WeakHashMap<ScheduledFuture<?>, Void>();
                     futures.put(executionscheduledFutures);
                 }
 
                scheduledFutures.put(futurenull);
            }
        }
    }

    
Class used to keep track of the threads employed by this runner.
    private static class ExecutionWrapper implements Runnable {
        private final Thread mCurrentThread;
        private final Execution mExecution;
        private final Map<ThreadVoidmThreads;

        
Constructor.

Parameters:
wrapped the wrapped execution.
threads the map of runner threads.
        private ExecutionWrapper(@Nonnull final Execution wrapped,
                @Nonnull final Map<ThreadVoidthreads) {
             = wrapped;
             = threads;
             = Thread.currentThread();
        }
        public void run() {
            final Thread currentThread = Thread.currentThread();
            if (currentThread != ) {
                .put(currentThreadnull);
            }
            .run();
        }
    }
New to GrepCode? Check out our FAQ X