如何在Scala中执行多个任务? [英] How can I execute multiple tasks in Scala?

查看:150
本文介绍了如何在Scala中执行多个任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有50,000个任务,并希望用10个线程执行它们。
在Java中,我应该创建Executers.threadPool(10)并将runnable传递给然后等待处理所有。 Scala因为我理解对于这个任务特别有用,但我不能在docs中找到解决方案。

I have 50,000 tasks and want to execute them with 10 threads. In Java I should create Executers.threadPool(10) and pass runnable to is then wait to process all. Scala as I understand especially useful for that task, but I can't find solution in docs.

推荐答案

Scala 2.9.3及更高版本



使用 scala.concurrent.Future 类和相关的基础结构。 scala.concurrent.future 方法异步评估传递给它的块,并立即返回一个 Future [A] 异步计算。期货可以以多种非阻塞方式进行操作,包括映射,flatMapping,过滤,恢复错误等。

Scala 2.9.3 and later

THe simplest approach is to use the scala.concurrent.Future class and associated infrastructure. The scala.concurrent.future method asynchronously evaluates the block passed to it and immediately returns a Future[A] representing the asynchronous computation. Futures can be manipulated in a number of non-blocking ways, including mapping, flatMapping, filtering, recovering errors, etc.

例如,下面的示例创建了10个任务,其中每个任务睡眠任意时间,然后返回传递给它的值的平方。

For example, here's a sample that creates 10 tasks, where each tasks sleeps an arbitrary amount of time and then returns the square of the value passed to it.

import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

val tasks: Seq[Future[Int]] = for (i <- 1 to 10) yield future {
  println("Executing task " + i)
  Thread.sleep(i * 1000L)
  i * i
}

val aggregated: Future[Seq[Int]] = Future.sequence(tasks)

val squares: Seq[Int] = Await.result(aggregated, 15.seconds)
println("Squares: " + squares)

在这个例子中,我们首先创建一个单独的异步任务序列,当完成时,提供一个int 。然后我们使用 Future.sequence 将这些异步任务合并到单个异步任务中 - 交换 Future Seq 。最后,我们在等待结果时阻塞当前线程达15秒。在示例中,我们使用全局执行上下文,它由一个fork / join线程池支持。对于非平凡的示例,您可能需要使用特定于 ExecutionContext 的应用程序。

In this example, we first create a sequence of individual asynchronous tasks that, when complete, provide an int. We then use Future.sequence to combine those async tasks in to a single async task -- swapping the position of the Future and the Seq in the type. Finally, we block the current thread for up to 15 seconds while waiting for the result. In the example, we use the global execution context, which is backed by a fork/join thread pool. For non-trivial examples, you probably would want to use an application specific ExecutionContext.

避免在可能的时候。在 Future 类上还有其他组合器可以帮助以异步风格编程,包括 onSuccess onFailure onComplete

Generally, blocking should be avoided when at all possible. There are other combinators available on the Future class that can help program in an asynchronous style, including onSuccess, onFailure, and onComplete.

href =http://akka.io> Akka 库,它为Scala和Java提供基于角色的并发,并与 scala.concurrent 交互操作。

Also, consider investigating the Akka library, which provides actor-based concurrency for Scala and Java, and interoperates with scala.concurrent.

这种最简单的方法是使用Scala的Future类,的组成部分。 scala.actors.Futures.future方法为传递给它的块创建一个Future。然后可以使用scala.actors.Futures.awaitAll等待所有任务完成。

This simplest approach is to use Scala's Future class, which is a sub-component of the Actors framework. The scala.actors.Futures.future method creates a Future for the block passed to it. You can then use scala.actors.Futures.awaitAll to wait for all tasks to complete.

例如,下面的示例创建了10个任务,其中每个任务睡眠任意时间量,然后返回传递给它的值的平方。

For example, here's a sample that creates 10 tasks, where each tasks sleeps an arbitrary amount of time and then returns the square of the value passed to it.

import scala.actors.Futures._

val tasks = for (i <- 1 to 10) yield future {
  println("Executing task " + i)
  Thread.sleep(i * 1000L)
  i * i
}

val squares = awaitAll(20000L, tasks: _*)
println("Squares: " + squares)

这篇关于如何在Scala中执行多个任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆