gpt4 book ai didi

multithreading - 如何在 groovy 中使用多线程访问 1000 个端点?

转载 作者:行者123 更新时间:2023-12-02 13:50:23 61 4
gpt4 key购买 nike

我有要求打端点超过 1000 次 从网站获取一些数据。所以我读了一些教程来使用多线程 去实现它。但一次我只想使用 13 个线程 用同样的方法 .

所以基本上我正在使用执行人服务 一次运行 13 个线程:

ExecutorService threadPool = Executors.newFixedThreadPool(13);
for (int itLocation = 0; itLocation < locationList.size(); itLocation++) {
//some code like
ScraperService obj = new ScraperService(threadName,url)
threadPool.submit(obj);
}
threadPool.shutdown();

我的 Groovy 类命名为 刮刀服务实现 Runnable 接口(interface) .
@Override
void run() {
println("In run method...................")
try {
Thread.sleep(5000);
someMethod()
} catch (InterruptedException e) {
e.printStackTrace();
}
}

问题:

我的问题是我的 ExecutorService.submit(obj)ExecutorService.execute(obj)没有调用我的 Runnable 接口(interface)的 run() 方法。

在 Groovy/Grails 中:

还有一个执行器插件 Executor Plugin in grails但我没有找到任何合适的示例如何使用它。

最佳答案

GPars 非常适合这种类型的事情。

您的 ScraperService 可以只负责处理像下面这样的抓取数据,或者也可以获取它,无论如何。

import groovyx.gpars.GParsPool

def theEndpoint = 'http://www.bbc.co.uk'

def scraperService

GParsPool.withPool( 13 ) {
(1..1000).eachParallel {
scraperService.scrape theEndpoint.toURL().text
}
}

关于multithreading - 如何在 groovy 中使用多线程访问 1000 个端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180536/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com