Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Groovy Goodness: Interrupted Sleeping

DZone's Guide to

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.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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 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.

Image title

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
$

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
groovy ,sleep ,closure ,java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}