Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008, 2010 VMware Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: VMware Inc. - initial contribution /
 
 
 package org.eclipse.virgo.nano.core.internal.blueprint;
 
StandardTicker provides a heart-beat for tracking unanticipated delays. The heart-beat is configurable using a policy such as StandardTicker.ExponentialHeartBeatPolicy which lengthens the heart-beat interval exponentially over time until it reaches a fixed upper bound. On each heart-beat until the ticker is cancelled, an action is called.

Concurrent Semantics
This class is thread safe.

Parameters:
<V> the result type of the action taken on each heart-beat
 
 public final class StandardTicker<V> implements TickerCallable<V> {
 
     private static final long OVERDUE = -1;
 
     private final long creationTimeMillis;
 
     private long lastTickMillis;
 
     private final HeartBeatPolicy heartBeatPolicy;
 
     private long heartBeatIntervalMillis;
     
     private boolean tickedAtLeastOnce = false;
     
     private boolean cancelled = false;
     
     private ScheduledFuture<V> scheduledFuture;
 
     private final Callable<V> action;
 
 
     private Object monitor = new Object();

    
Create a Ticker with the given heart-beat policy, action to be called on each tick, and executor service for scheduling ticks and set it ticking.

Parameters:
<V> the action's result type
heartBeatPolicy a policy which determines the possibly variable intervals between heartbeats
action the thread safe action to be called on each tick
scheduledExecutorService the executor service for scheduling ticks
Returns:
the constructed and ticking ticker
 
     public static <V> Ticker createTicker(HeartBeatPolicy heartBeatPolicyCallable<V> actionScheduledExecutorService scheduledExecutorService) {
         StandardTicker<V> ticker = new StandardTicker<V>(heartBeatPolicyactionscheduledExecutorService);
         ticker.start();
         return ticker;
     }

    
Create a Ticker with an exponential heart beat policy and a given action to be called on each tick and an executor service for scheduling ticks and set it ticking. The exponential heart beat policy has the given initial interval which then increases by the given percentage on each tick until the given maximum interval is reached.

Parameters:
<V> the action's result type
initialHeartbeatIntervalMillis the initial interval
heartBeatIncreasePercentage the percentage increase on each tick
maxHeartBeatIntervalMillis the maximum interval
action the thread safe action to be called on each tick
scheduledExecutorService the executor service for scheduling ticks
Returns:
the constructed and ticking ticker
 
     public static <V> Ticker createExponentialTicker(long initialHeartbeatIntervalMillislong heartBeatIncreasePercentage,
         long maxHeartBeatIntervalMillisCallable<V> actionScheduledExecutorService scheduledExecutorService) {
         return createTicker(new ExponentialHeartBeatPolicy(initialHeartbeatIntervalMillisheartBeatIncreasePercentagemaxHeartBeatIntervalMillis),
             actionscheduledExecutorService);
     }

    
Construct a Ticker with the given heart beat policy, action to be called on each tick, and executor service for scheduling ticks.

Parameters:
heartBeatPolicy a policy which determines the possibly variable intervals between heartbeats
action the thread safe action to be called on each tick
scheduledExecutorService the executor service for scheduling ticks
    private StandardTicker(HeartBeatPolicy heartBeatPolicyCallable<V> actionScheduledExecutorService scheduledExecutorService) {
        this. = heartBeatPolicy;
        this. = System.currentTimeMillis();
        this. = this.;
        this. = action;
        this. = scheduledExecutorService;
    }

    
Start this ticker ticking.
    private void start() {
        if (getIntervalToNextTickMillis() == ) {
            try {
                this.call();
            } catch (Exception e) {
            }
        } else {
            scheduleNextTick();
        }
    }

    
Schedule the next tick of this ticker.
    private void scheduleNextTick() {
        synchronized (this.) {
            if (!this.) {
            }
        }
    }

    
    public V call() throws Exception {
        boolean ticked = tick();
        scheduleNextTick();
        return ticked ? this..call() : null;
    }

    
    public boolean cancel() {
        synchronized (this.) {
            this. = true;
            this..cancel(true);
            return this.;
        }
    }

    
Determine whether a tick is due and, if so, update the ticker state to count down to the next tick and return true. If no tick is due, do not update the ticker state and return false.

Returns:
true if and only if the ticker tickedAtLeastOnce
    private boolean tick() {
        synchronized (this.) {
            boolean ticked = false;
            if (! && getIntervalToNextTickMillis() == ) {
                ticked = true;
                this. = getCurrentTimeMillis();
                this. = this..getNextHeartBeatIntervalMillis();
            }
            this. = this. || ticked;
            return ticked;
        }
    }

    
Get the current time. Pre-condition: the monitor must be held. Post-condition: result >= this.lastTickMillis.

Returns:
the current time in milliseconds
    private long getCurrentTimeMillis() {
        long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis < this.) {
        	throw new IllegalArgumentException("Time must not go backwards");
        }
        return currentTimeMillis;
    }

    
Get the time interval until the next tick is due, or OVERDUE if the next tick is overdue.

Returns:
the time interval in milliseconds, or OVERDUE if the next tick is overdue
    private long getIntervalToNextTickMillis() {
        synchronized (this.) {
            long intervalSinceLastTickMillis = getCurrentTimeMillis() - this.;
            return intervalSinceLastTickMillis < this. ? this. - intervalSinceLastTickMillis : ;
        }
    }

    
StandardTicker.ExponentialHeartBeatPolicy is a Ticker.HeartBeatPolicy which returns intervals starting with a given initial interval and increasing by a given percentage up to a given maximum interval.

Concurrent Semantics
This class is thread safe.

    private final static class ExponentialHeartBeatPolicy implements HeartBeatPolicy {
        private final long maxHeartBeatIntervalMillis;
        private final long heartBeatIncreasePercentage;
        private AtomicLong heartBeatIntervalMillis;

        
Construct a HeartBeatPolicy which the given initial interval which then increases by the given percentage on each tick until the given maximum interval is reached.

Parameters:
initialHeartbeatIntervalMillis the initial interval
heartBeatIncreasePercentage the percentage increase on each tick
maxHeartBeatIntervalMillis the maximum interval
        public ExponentialHeartBeatPolicy(long initialHeartbeatIntervalMillislong heartBeatIncreasePercentagelong maxHeartBeatIntervalMillis) {
            this. = new AtomicLong(initialHeartbeatIntervalMillis);
            this. = heartBeatIncreasePercentage;
            this. = maxHeartBeatIntervalMillis;
        }

        
        public long getNextHeartBeatIntervalMillis() {
            boolean success = false;
            long nextHeartBeatIntervalMillis = 0;
            while (!success) {
                nextHeartBeatIntervalMillis = this..get();
                if (nextHeartBeatIntervalMillis < ) {
                    long newHeartBeatIntervalMillis = Math.min((nextHeartBeatIntervalMillis * (100 + )) / 100,
                        );
                    success = this..compareAndSet(nextHeartBeatIntervalMillisnewHeartBeatIntervalMillis);
                } else {
                    success = true;
                }
            }
            return nextHeartBeatIntervalMillis;
        }
    }
New to GrepCode? Check out our FAQ X