List multiprocessing in groovy

156 Views Asked by At

I have a list of URLs in groovy for which I would like to start a new thread for each. I would like to know the groovy way of doing this list multiprocessing. Suppose I have a list of urls I wish to fetch data from concurrently.

def urls = ["https://stackexchange.com","https://amazon.com","https://bing.com","https://google.com"]
1

There are 1 best solutions below

9
Brian On BEST ANSWER
import groovyx.gpars.GParsPool

def urls = ["https://stackexchange.com","https://amazon.com","https://bing.com","https://google.com"]

GParsPool.withPool( urls.size() ) { urls.eachParallel { url ->
try {
        // test if url is reachable
        response = url.toURL().text
        println url + " connected OK"
    } 
catch (Exception e) {  println e }
    }
}