我正在寻找一种简单的方法来并行启动多个任务并等待所有任务完成。
考虑这个 c# 示例:
private static void Main(string[] args)
{
var urlList = new[] {"http://www.microsoft.com/", "http://www.google.com/", "http://www.apple.com/" };
var result = GetHtml(urlList);
}
private static List<string> GetHtml(string[] urlList)
{
var tasks = new List<Task>();
var output = new List<string>();
foreach (var url in urlList)
{
var task = new Task(() =>
{
var html = new WebClient().DownloadString(url);
output.Add(html);
});
tasks.Add(task);
//starts task in a separate thread (doesn't block anything)
task.Start();
}
//waits for all tasks (running in parallel) to complete before exiting method
Task.WaitAll(tasks.ToArray());
return output;
}
GetHtml 方法并行下载多个网页并返回一个 html 字符串列表。
如何使用 kotlin/anko 实现这一目标?
private fun GetHtml(urlList: Array<String>): ArrayList<String> {
val tasks = ArrayList<Future<Unit>>()
val output = ArrayList<String>()
for (url in urlList) {
val task = async() {
//some java-code that downloads html from <url>, doesn't matter for now
output.add("html for $url")
}
tasks.add(task)
}
//this is NOT parallel execution
for (task in tasks) {
task.get()
}
//tasks.getall() ??
return output
}