Search

Dark theme | Light theme

October 24, 2016

Groovy Goodness: Interrupted Sleeping

Groovy 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 milli seconds. The implementation of the method will always wait for he given amount of milli seconds even if interrupted. But we can add a closure as 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 milli seconds we intercept the thread. The sleep method still wait 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 return 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
$

Written with Groovy 2.4.7.