Groovy multithreading

I am new to groovy / grails.

How to implement a thread for this code. It had 2500 URLs, and it took several hours to check each URL.

so I decided to implement multithreading for this:

Here is my sample code:

def urls = [
  "http://www.wordpress.com",
  "http://67.192.103.225/QRA.Public/" ,
  "http://www.subaru.com",
  "http://baldwinfilter.com/products/start.html"
]

def up = urls.collect { ur ->
    try {
        def url = new URL(ur)
        def connection = url.openConnection()
        if (connection.responseCode == 200) {
            return true
        } else {
            return false
        }
    } catch (Exception e) {
        return false
    } 
}

For this code, I need to implement multithreading .
Can anyone suggest me a code.

thanks in advance,
shri.

+3
source share
5 answers

I would look at the Groovy Parallel Systems library . In particular, I think the section

+11
source

Groovy , ExecutorService , .

+3

, URL- .

class URLReader implements Runnable
{
    def valid
    def url

    URLReader( url ) {
        this.url = url
    }

    void run() {
        try {
            def connection = url.toURL().openConnection()
            valid = ( connection.responseCode == 200 ) as Boolean
        } catch ( Exception e ) {
            println e.message
            valid = Boolean.FALSE
        }
    }
}
def reader = new URLReader( "http://www.google.com" )
new Thread( reader ).start()
while ( reader.valid == null )
{
    Thread.sleep( 500 )
}
println "valid: ${reader.valid}"

. null, Boolean.TRUE, Boolean.FALSE. , . URL-, , /, , URL- .

+1

, .


import java.util.concurrent.*

//Thread number
THREADS = 100
pool = Executors.newFixedThreadPool(THREADS)
defer = { c -> pool.submit(c as Callable) }

def urls = [
  "http://www.wordpress.com",
  "http://www.subaru.com",
]

def getUrl = { url ->
  def connection = url.openConnection()
  if (connection.responseCode == 200) {
    return true
  } else {
    return false
  }
}


def up = urls.collect { ur ->
    try {
        def url = new URL(ur)
    defer{ getUrl(url) }.get()
    } catch (Exception e) {
        return false
    } 
}

println up
pool.shutdown()
+1

:

class ValidateLinks extends Thread{
    def valid
    def url

    ValidateLinks( url ) {
        this.url = url
    }

    void run() {
        try {
            def connection = url.toURL().openConnection()
        connection.setConnectTimeout(5000)
            valid = ( connection.responseCode == 200 ) as Boolean
        } catch ( Exception e ) {
            println url + "-" + e.message
            valid = Boolean.FALSE
        }
    }
}

def threads = [];
urls.each { ur ->
def reader = new ValidateLinks(ur.site_url)
reader.start()
threads.add(reader);
}

while (threads.size() > 0) {

        for(int i =0; i < threads.size();i++) {
                def tr = threads.get(i);
                if (!tr.isAlive()) {
                        println "URL : " + tr.url  + "Valid " + tr.valid
                        threads.remove(i);
                        i--;

                }
        }
}
0

Source: https://habr.com/ru/post/1739970/


All Articles