Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Groovy - Advanced Topics

24. Advanced Topics (Concurrency, Groovy GDK)

Concurrency in Groovy:

Concurrency is the management of multiple tasks or threads in a way that they can run independently and efficiently. Groovy provides powerful concurrency support, especially when working with Java's threading capabilities. Let's explore some aspects of concurrency in Groovy:

1. Threads and Runnable:

You can create and run threads in Groovy using Java's 'Thread' class or implementing the 'Runnable' interface.

Example:
def thread = new Thread({
    for (int i = 0; i < 5; i++) {
        println "Thread 1: $i"
    }
})

thread.start()
 

2. Groovy's @ThreadInterrupt:

Groovy provides the '@ThreadInterrupt' annotation, which simplifies interrupting threads gracefully.

Example:
import groovy.transform.ThreadInterrupt

@ThreadInterrupt
def myThread = {
    for (int i = 0; i < 5; i++) {
        println "Thread 2: $i"
        Thread.sleep(1000)
    }
}

def thread = new Thread(myThread)
thread.start()

Thread.sleep(3000)
thread.interrupt()
 

Groovy GDK (Groovy Development Kit):

The Groovy GDK extends the capabilities of Java's standard libraries, making it easier to work with common tasks. It adds numerous methods and simplifies various operations. Let's explore some aspects of the Groovy GDK:

1. Collection Enhancements:

Groovy adds several convenient methods to work with collections, such as 'each', 'findAll', 'collect', and 'groupBy'.

Example:
def numbers = [1, 2, 3, 4, 5]

// Using GDK methods
numbers.each { println it }
def evenNumbers = numbers.findAll { it % 2 == 0 }
def doubled = numbers.collect { it * 2 }
def grouped = numbers.groupBy { it % 2 }
 

2. String Manipulation:

Groovy simplifies string manipulation with methods like 'replaceAll', 'toUpperCase', and 'tokenize'.

Example:
def text = "Hello, Groovy!"

// Using GDK methods
def upperText = text.toUpperCase()
def replacedText = text.replaceAll("Groovy", "World")
def tokens = text.tokenize(", ")
 

3. File Operations:

Groovy makes working with files more straightforward with methods like 'eachLine', 'readLines', and 'withReader'.

Example:
// Using GDK methods
new File("myFile.txt").eachLine { println it }
def lines = new File("myFile.txt").readLines()
 

4.  XML and JSON Handling:

Groovy provides native support for working with XML and JSON data structures, simplifying parsing and manipulation.

Example (XML):
def xml = '''<book>
    <title>Groovy in Action</title>
    <author>Dierk König</author>
</book>'''

def parsedXml = new XmlSlurper().parseText(xml)

// Accessing XML elements
def title = parsedXml.title.text()
   
 
Example (JSON):
def json = '{"name": "Alice", "age": 30}'
def parsedJson = new JsonSlurper().parseText(json)

// Accessing JSON properties
def name = parsedJson.name
 

These advanced topics in Groovy, including concurrency and the Groovy GDK, provide powerful tools to make your code more efficient, readable, and expressive. By mastering these concepts, you can leverage Groovy's capabilities to write high-quality and feature-rich applications.