Skip to content

Instantly share code, notes, and snippets.

@kdabir
Last active November 20, 2017 08:26
Show Gist options
  • Save kdabir/3176945 to your computer and use it in GitHub Desktop.
Save kdabir/3176945 to your computer and use it in GitHub Desktop.
Creating and Scheduling a TimerTask in groovy

Creating a TimerTask can not get easier than this thanks to groovy.

  • import java.util.timer.* is not required as java.util is already imported.
  • the run() is implemented as closure

To Run this:

groovy https://gist.github.com/kdabir/3176945/raw/timer_example.groovy

def count = 0
new Timer().schedule({
println count++
} as TimerTask, 1000, 3000) //magic numbers are initial-delay & repeat-interval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment