Groovy Goodness: Interrupted Sleeping
The sleep method is explained with the use of closures. See how the sleep method works and how you can use interrupts to wake your code up when you want to.
Join the DZone community and get the full member experience.
Join For FreeGroovy adds a lot of useful methods to the Java JDK classes. One of them is the sleep
method that is added to all objects. With the sleep
method, we can add a pause to our code. The sleep
method accepts a sleep time in milliseconds. The implementation of the method will always wait for the given amount of milliseconds, even if interrupted. But we can add a closure as an extra argument, which is invoked when the sleep
method is interrupted. We should return true
for the closure to really interrupt, otherwise we use false
.
In the following example, we use the sleep
method to pause the bedtime
method of the User
class. We run the bedtime
method in a thread, and after 2000 milliseconds, we intercept the thread. The sleep
method still waits for 5 seconds, before ending:
class User {
String username
void bedtime() {
long start = System.currentTimeMillis()
println 'Sleeping'
sleep(5000)
long slept = System.currentTimeMillis() - start
println "Awake after $slept ms"
}
}
def user = new User(username: 'mrhaki')
// Run bedtime method in thread.
def bedtime = Thread.start {
user.bedtime()
}
def alarm = new Timer()
alarm.runAfter(2000) {
println 'BEEP BEEP'
// Interrupt thread with bedtime method.
bedtime.interrupt()
}
bedtime.join()
When we run this script, we get the following output:
$ groovy user.groovy
Sleeping
BEEP BEEP
Awake after 5002 ms
$
Next, we use the sleep
method with a closure. The closure prints a message and returns false
, so the sleep
method still only ends after 5 seconds:
class User {
String username
void bedtime() {
long start = System.currentTimeMillis()
println 'Sleeping'
sleep(5000) { e ->
assert e in InterruptedException
println 'What is that noise?'
false // keep on sleeping
}
long slept = System.currentTimeMillis() - start
println "Awake after $slept ms"
}
}
def user = new User(username: 'mrhaki')
// Run bedtime method in thread.
def bedtime = Thread.start {
user.bedtime()
}
def alarm = new Timer()
alarm.runAfter(2000) {
println 'BEEP BEEP'
// Interrupt thread with bedtime method.
bedtime.interrupt()
}
bedtime.join()
Let's run this script, and we see the output from the closure we passed to the sleep
method:
$ groovy user.groovy
Sleeping
BEEP BEEP
What is that noise?
Awake after 5005 ms
$
Finally, we use a closure as argument for the sleep
method, but this time, we return true
to indicate the sleep
method must stop:
class User {
String username
void bedtime() {
long start = System.currentTimeMillis()
println 'Sleeping'
sleep(5000) { e ->
assert e in InterruptedException
println 'Yeah, yeah, I am awake...'
true // stop on sleeping
}
long slept = System.currentTimeMillis() - start
println "Awake after $slept ms"
}
}
def user = new User(username: 'mrhaki')
// Run bedtime method in thread.
def bedtime = Thread.start {
user.bedtime()
}
def alarm = new Timer()
alarm.runAfter(2000) {
println 'BEEP BEEP'
// Interrupt thread with bedtime method.
bedtime.interrupt()
}
bedtime.join()
When we run this script, we notice we are awake after 2 seconds:
$ groovy user.groovy
Sleeping
BEEP BEEP
Yeah, yeah, I am awake...
Awake after 2001 ms
$
Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments