网址的常规线程

时间:2010-04-27 10:21:23

标签: groovy multithreading

我编写了使用线程测试网址的逻辑。 这适用于较少数量的网址和失败,需要检查超过400个网址。

类URL扩展线程{
    def有效     def url

URL( url ) {
    this.url = url
}

void run() {
    try {
        def connection = url.toURL().openConnection()
    connection.setConnectTimeout(10000)
        if(connection.responseCode == 200 ){
            valid = Boolean.TRUE
        }else{
            valid = Boolean.FALSE
        }
    } catch ( Exception e ) {
        valid = Boolean.FALSE
    }
}
}



    def threads = [];
    urls.each { ur ->
       def reader = new URL(ur)
       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()) {
                if(tr.valid == true){
                  threads.remove(i);
                  i--; 
            }else{
              threads.remove(i);
              i--;
            }
        }
     }

任何人都可以告诉我如何优化逻辑以及我出错的地方。

提前感谢。

2 个答案:

答案 0 :(得分:2)

您是否考虑过使用java.util.concurrent助手?它允许在更高抽象级别进行多线程编程。有一个简单的接口可以在线程池中运行并行任务,这比仅为n个任务创建n个线程并希望最好的更容易管理和调整。

您的代码最终会看起来像这样,您可以调整nThreads直到获得最佳性能:

import java.util.concurrent.*

def nThreads = 1000
def pool = Executors.newFixedThreadPool(nThreads)
urls.each { url ->
    pool.submit(url)
}
def timeout = 60
pool.awaitTermination(timeout, TimeUnit.SECONDS)

答案 1 :(得分:1)

使用ataylor的建议和你的代码,我得到了这个:

import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class MyURL implements Runnable {
  def valid
  def url

  void run() {
    try {
      url.toURL().openConnection().with {
        connectTimeout = 10000
        if( responseCode == 200  ) {
          valid = true
        }
        else {
          valid = false
        }
        disconnect()
      }
    }
    catch( e ) {
      valid = false
    }
  }
}

// A list of URLs to check
def urls = [ 'http://www.google.com',
             'http://stackoverflow.com/questions/2720325/groovy-thread-for-urls',
             'http://www.nonexistanturlfortesting.co.ch/whatever' ]

// How many threads to kick off
def nThreads = 3
def pool = Executors.newFixedThreadPool( nThreads )

// Construct a list of the URL objects we're running, submitted to the pool
def results = urls.inject( [] ) { list, url ->
  def u = new MyURL( url:url )
  pool.submit u
  list << u
}

// Wait for the poolclose when all threads are completed
def timeout = 10
pool.shutdown()
pool.awaitTermination( timeout, TimeUnit.SECONDS )

// Print our results
results.each {
  println "$it.url : $it.valid"
}

打印出来:

http://www.google.com : true
http://stackoverflow.com/questions/2720325/groovy-thread-for-urls : true
http://www.nonexistanturlfortesting.co.ch/whatever : false

我将类名更改为MyURL而不是URL,因为当您开始使用java.net.URL类时,它更可能避免出现问题