java - 从不同线程的多个任务中检索数据

标签 java multithreading threadpool

晚上好,

我有一个不同 URL 的列表(大约 500 个),我可以通过此方法获取内容

public static String getWebContent(URL url){
 // create URL, build HTTPConnection, getContent of page
}

在此之后,我有另一种方法,可以获取内容的值等。 这时候我是这样做的:

List<URL> urls = new ArrayList<>();
List<String> webcontents = new ArrayList<>();
    for(URL url : urls){
         webcontents.add(getWebContent(url));
    }
// Futher methods to extract values from the webcontents

但这实际上需要很多时间,因为只有一个线程在做这件事。我想让它成为多线程,但我不确定最好的方法是什么。

首先,我需要每个线程的返回值,我应该为其实现Callable而不是Runnable吗?

如何使用不同的线程运行该方法,是否应该有一个以索引 0 开头,一个以索引 50 开头,等等?当他们完成一个 URL 后,他们将一个标志设置为 true?这就是我的方法,但我认为它不是很有效。如果第一个网站有很多内容,第一个线程可能比其他线程花费更长的时间。

当每个线程完成后,我如何将我的数据返回到一个列表?像这样吗?

List<String> webcontent = new ArrayList<>();
    if(!t1.isAlive() && !t2.isAlive()){
        webcontent.add(t1.getData());
        webcontent.add(t2.getData());
    }

我希望你能理解我的问题并给我一些提示:)非常感谢

最佳答案

您可以使用 ExecutorCompletionService 在任务完成时检索任务。

List<URL> urls = ...; // Create this list somehow
ExecutorCompletionService<String> service =
    new ExecutorCompletionService<String>(Executors.newFixedThreadPool(10));
for (URL url: urls) {
    service.submit(new GetWebContentCallable(url)); // you need to define the GetWebContentCallable
}
int remainingTasks = urls.size();
while (remainingTasks > 0) {
    String nextResult = service.take();
    processResult(nextResult); // you define processResult
    remainingTasks -= 1;
}

关于java - 从不同线程的多个任务中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19670809/

相关文章:

java - Spring 3 Controller - 通过流程维护模型

java - 天花板和地板之间奇怪的平等

java - 通过多线程的并发方法

c++ - 如何在 gdb 控制台模式下捕获新线程?

调用 free 导致程序抛出异常

java - ForkJoinPool.commonPool() 的线程拒绝策略是什么?

multithreading - IIS上下文中的线程池

java - 如果发生异常,ThreadPoolExecutor::afterExecute 会返回可运行吗?

java - Java thread.stop() 是如何工作的?

Java .xml 创建可绘制按钮背景