Logo

dev-resources.site

for different kinds of informations.

Processing in parallel with Groovy

Published at
4/3/2023
Categories
groovy
Author
ou_ryperd
Categories
1 categories in total
groovy
open
Author
9 person written this
ou_ryperd
open
Processing in parallel with Groovy

I had to place thousands of messages on a queue and collect each response by an ID. This is how I used multiprocessing to do that, so that messages were processed in parallel to make it complete faster:

//lists to keep the thread instances
List threads1 = []
List threads2 = []

//first task (e.g. putting on Q)
def doThis = { i, j ->
    sleep(randNum(100))
    println "doThis(${i}) thread: ${j}"
}

//second task (e.g. getting from Q)
def doThat = { i, j ->
    sleep(randNum(20000))
    println "doThat(${i}) thread: ${j}"
}

//call first task (e.g. for eachRow from DB, put msg on Q)
20.times { i ->
    def t1
    t1 = Thread.start { doThis(i, t1.name) }
    threads1.add(t1)
}

//wait for all task 1 threads to end
for (thread in threads1) {
    thread.join()
    println "doThis joined: ${thread.name}"
}

//call second task (e.g. for each correlation ID, get msg from Q)
20.times { i ->
    def t2
    t2 = Thread.start { doThat(i, t2.name) }
    threads2.add(t2)
}

//wait for all task 2 threads to end
for (thread in threads2) {
    thread.join()
    println "doThat joined: ${thread.name}"
}

println "All threads done"

def randNum(maxSize) { //create random wait times
    return Math.abs(new Random().nextInt() % maxSize) + 1 
}
Enter fullscreen mode Exit fullscreen mode
groovy Article's
30 articles in total
Favicon
Exploring Groovy: Features and Advantages Over Java
Favicon
Tasting Groogle
Favicon
Machine Learning with Spark and Groovy
Favicon
Groogle 4.0.0 (Google DSL)
Favicon
Tutorial: Learn how to use the H2 Database with Spring Boot! 🤔
Favicon
Mocking with Groovy
Favicon
Check for newer versions of dependencies in pom.xml
Favicon
Setting up linters in Gitlab CI for C++ and Groovy / Jenkins code
Favicon
Machine Learning con Groovy
Favicon
Groovy 🎷 Cheat Sheet - 01 Say "Hello" from Groovy
Favicon
Advice needed
Favicon
HackTheBox - Writeup Builder [Retired]
Favicon
The golden age of Kotlin and its uncertain future
Favicon
Probando a firmar documentos con Docuten
Favicon
Groogle al rescate
Favicon
Tech Watch #3 — October, 20, 2023
Favicon
Swagger-Operator, let groovy operate your cluster
Favicon
Nextflow: Organizando fotos por geoposicion
Favicon
🎶 Groovy: The Dynamic and Versatile JVM Language! 🚀
Favicon
FediSearch
Favicon
Introduction To Jenkins Shared Libraries
Favicon
Remove items matching a pattern from a list in Groovy
Favicon
Processing in parallel with Groovy
Favicon
Wednesday Links - Edition 2023-05-03 🇵🇱📜
Favicon
Wednesday Links - Edition 2023-04-26
Favicon
An Introduction to Jenkins Pipeline: Simplifying Continuous Integration and Delivery with Examples
Favicon
Graalvanizando un script de Groovy
Favicon
Passage of time calculations in Groovy
Favicon
Write a method like Python's string.printable in Groovy
Favicon
Neat time and date manipulation with Groovy

Featured ones: