org.jdesktop.wonderland.client.utils
Class Throttle

java.lang.Object
  extended by org.jdesktop.wonderland.client.utils.Throttle

public class Throttle
extends java.lang.Object

A throttle can be used to schedule tasks with a fixed maximum rate. For example, messages to the server can be scheduled with a 500ms delay, meaning at most two would be sent per second.

When tasks are scheduled more frequently than the rate of the throtte, some tasks will be dropped. In all cases, the most recently submitted task will be executed, but older tasks will be dropped.

All throttles in the system share a single, underlying thread pool. This makes them efficient, but means that individual tasks should happen quickly. Scheduling too many slow tasks will slow down the thread pool for the whole system.

Author:
jkaplan

Constructor Summary
Throttle(long rate, java.util.concurrent.TimeUnit rateUnit)
          Create a new throttle that executes at most times every .
 
Method Summary
 void clear()
          Clears any outstanding tasks, and cancels any pending sends
 boolean schedule(java.lang.Runnable task)
          Schedule a task.
 void setRate(long rate, java.util.concurrent.TimeUnit rateUnit)
          Set the rate of this throttle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Throttle

public Throttle(long rate,
                java.util.concurrent.TimeUnit rateUnit)
Create a new throttle that executes at most times every . For example, if the rate is 2 and the TimeUnit is SECONDS, this will create a throttle that executes at most 2 times per second, or every 500 milliseconds. If the rate is 2 and the TimeUnit is MINUTES, this will create a throttle that executes at most 2 times per minute, or every 30 seconds.

Parameters:
rate - the rate to schedule at
rateUnit - the unit of the given rate
Method Detail

setRate

public void setRate(long rate,
                    java.util.concurrent.TimeUnit rateUnit)
Set the rate of this throttle

Parameters:
rate - the rate to schedule at
rateUnit - the unit of the given rate

schedule

public boolean schedule(java.lang.Runnable task)
Schedule a task. The task will execute immediately if no tasks have executed for more than the requested period. If a task has executed within the requested period, this task will be scheduled for execution once the delay has expired.

Parameters:
task - the task to schedule
Returns:
true if the task was run immediately, or false if it was delayed

clear

public void clear()
Clears any outstanding tasks, and cancels any pending sends



Open Wonderland - http://openwonderland.org