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;
 
 
 
Runner implementation throttling the number of running executions so to keep it under a specified limit.

Created by davide on 18/07/15.

 
 class ThrottlingRunner implements Runner {
 
 
     private final int mMaxRunning;
 
     private final Object mMutex = new Object();
 
     private final LinkedList<PendingExecutionmQueue = new LinkedList<PendingExecution>();
 
     private final Runner mRunner;
 
     private int mRunningCount;

    
Constructor.

Parameters:
wrapped the wrapped instance.
maxExecutions the maximum number of running executions.
Throws:
java.lang.IllegalArgumentException if the specified max number is less than 1.
 
     @SuppressWarnings("ConstantConditions")
     ThrottlingRunner(@Nonnull final Runner wrappedfinal int maxExecutions) {
 
         if (wrapped == null) {
 
             throw new NullPointerException("the wrapped runner must not be null");
         }
 
         if (maxExecutions < 1) {
 
             throw new IllegalArgumentException(
                     "the maximum number of running executions must be at least 1, while it was: "
                             + maxExecutions);
         }
 
          = wrapped;
          = maxExecutions;
     }
 
     public void cancel(@Nonnull final Execution execution) {
 
         ThrottlingExecution throttlingExecution = null;
 
         synchronized () {
 
             final Iterator<PendingExecutioniterator = .iterator();
 
             while (iterator.hasNext()) {
 
                 final PendingExecution pendingExecution = iterator.next();
 
                 if (pendingExecution.mExecution == execution) {
 
                     iterator.remove();
                 }
             }
 
             final WeakReference<ThrottlingExecutionexecutionReference =
                     .get(execution);
 
             if (executionReference != null) {
 
                 throttlingExecution = executionReference.get();
             }
         }
 
         if (throttlingExecution != null) {
            .cancel(throttlingExecution);
        }
    }
    public boolean isExecutionThread() {
        return .isExecutionThread();
    }
    public void run(@Nonnull final Execution executionfinal long delay,
            @Nonnull final TimeUnit timeUnit) {
        ThrottlingExecution throttlingExecution = null;
        synchronized () {
            final LinkedList<PendingExecutionqueue = ;
            if (( + queue.size()) >= ) {
                queue.add(new PendingExecution(executiondelaytimeUnit));
            } else {
                throttlingExecution = getThrottlingExecution(execution);
            }
        }
        if (throttlingExecution != null) {
            .run(throttlingExecutiondelaytimeUnit);
        }
    }
    @Nonnull
    private ThrottlingExecution getThrottlingExecution(@Nonnull final Execution execution) {
        final WeakReference<ThrottlingExecutionexecutionReference = .get(execution);
        ThrottlingExecution throttlingExecution =
                (executionReference != null) ? executionReference.get() : null;
        if (throttlingExecution == null) {
            throttlingExecution = new ThrottlingExecution(execution);
            if (execution.mayBeCanceled()) {
                .put(execution,
                                new WeakReference<ThrottlingExecution>(throttlingExecution));
            }
        }
        return throttlingExecution;
    }

    
Pending execution implementation.
    private class PendingExecution extends TemplateExecution {
        private final long mDelay;
        private final Execution mExecution;
        private final TimeUnit mTimeUnit;

        
Constructor.

Parameters:
execution the execution.
delay the execution delay.
timeUnit the delay time unit.
        private PendingExecution(@Nonnull final Execution executionfinal long delay,
                @Nonnull final TimeUnit timeUnit) {
             = execution;
             = delay;
             = timeUnit;
        }
        public void run() {
            final ThrottlingExecution throttlingExecution;
            synchronized () {
                throttlingExecution = getThrottlingExecution();
            }
            .run(throttlingExecution);
        }
    }

    
Execution used to dequeue and run pending executions, when the maximum running count allows it.
    private class ThrottlingExecution implements Execution {
        private final Execution mExecution;

        
Constructor.

Parameters:
execution the execution.
        private ThrottlingExecution(@Nonnull final Execution execution) {
             = execution;
        }
        public boolean mayBeCanceled() {
            return .mayBeCanceled();
        }
        public void run() {
            final int maxRunning = ;
            final Execution execution = ;
            final LinkedList<PendingExecutionqueue = ;
            synchronized () {
                if ( >= maxRunning) {
                    queue.addFirst(new PendingExecution(execution, 0, .));
                    return;
                }
                ++;
            }
            try {
                execution.run();
            } finally {
                PendingExecution pendingExecution = null;
                synchronized () {
                    if ((-- < maxRunning) && !queue.isEmpty()) {
                        pendingExecution = queue.removeFirst();
                    }
                }
                if (pendingExecution != null) {
                    pendingExecution.run();
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X