migrator / guava-libraries

Guava: Google Core Libraries for Java 1.6+
0 stars 0 forks source link

"Throttle" for slowing down rapidly submitted Runnables #13

Open migrator opened 10 years ago

migrator commented 10 years ago

From the mailing-list:

Especially in GUI-applications I often find myself in the need for a throttle which executes a Runnable only after a certain time has passed and only the last Runnable that has been scheduled.

Example: In SWT, when the user resizes a window or a table-column I store that information. SWT has a Listener for the resize-event which fires basically for every pixel that is moved, so one resize fires potentially hundreds of events, saving that every time would be slow and I'm only interested in the last value anyway.

I implemented an Executor [0] that can give me an instance of Throttle [1] with a specified timeout..

When a Runnable is submitted to/via the Throttle it is only executed after the timeout has passed. Within this time, every newly submitted Runnable replaces the one waiting.

[0] https://github.com/fab1an/appkit/blob/master/src/main/java/org/appkit/concurrent/SmartExecutor.java [1] https://fab1an.github.io/appkit/javadoc/org/appkit/concurrent/Throttle.html

relevance: 2