单词" 序列"表示一个接一个的系列动作。
object Test {
def main(args: Array[String]) {
def producer() = {
val list = Seq(
future { println("startFirst"); Thread.sleep(3000); println("stopFirst") },
future { println("startSecond"); Thread.sleep(1000); println("stopSecond") }
)
Future.sequence(list)
}
Await.result(producer, Duration.Inf)
}
}
因此我希望打印这个程序:
startFirst
stopFirst
startSecond
stopSecond
甚至:
startSecond
stopSecond
startFirst
stopFirst
但不是(因为它发生):
startFirst
startSecond
stopSecond
stopFirst
为什么不调用此方法Future.parallel()
?
我应该用什么来保证Seq
期货中的所有期货都是连续触发(而不是并行)?
答案 0 :(得分:34)
期货同时运行,因为它们已同时启动:)。 要按顺序运行它们,您需要使用flatMap:
Future { println("startFirst");
Thread.sleep(3000);
println("stopFirst")
}.flatMap{
_ => Future {
println("startSecond");
Thread.sleep(1000);
println("stopSecond")
}
}
Future.sequence只是变成Seq[Future[T]] => Future[Seq[T]]
,这意味着收集所有已经开始的未来的结果并将其放在未来。
答案 1 :(得分:10)
对原始Future.sequence的一点改动将使未来的执行序列化:
def seq[A, M[X] <: TraversableOnce[X]](in: M[() => Future[A]])(implicit cbf: CanBuildFrom[M[()=>Future[A]], A, M[A]], executor: ExecutionContext): Future[M[A]] = {
in.foldLeft(Future.successful(cbf(in))) {
(fr, ffa) => for (r <- fr; a <- ffa()) yield (r += a)
} map (_.result())
}
,您的代码将如下所示:
object Test {
def main(args: Array[String]) {
def producer() = {
val list = Seq(
{() => future { println("startFirst"); Thread.sleep(3000); println("stopFirst") }},
{() => future { println("startSecond"); Thread.sleep(1000); println("stopSecond") }}
)
FutureExt.seq(list)
}
Await.result(producer, Duration.Inf)
}
}
这与原始代码非常相似,并且与原始Future.sequence()
具有相同的结果集合答案 2 :(得分:4)
线性化:
import scala.concurrent._
import scala.collection.mutable.Builder
import scala.collection.generic.CanBuildFrom
import language.higherKinds
/**
* Linearize asynchronously applies a given function in-order to a sequence of values, producing a Future with the result of the function applications.
* Execution of subsequent entries will be aborted if an exception is thrown in the application of the function.
*/
def linearize[T, U, C[T] <: Traversable[T]](s: C[T])(f: T => U)(implicit cbf: CanBuildFrom[C[T], U, C[U]], e: ExecutionContext): Future[C[U]] = {
def next(i: Iterator[T], b: Builder[U, C[U]]): Future[C[U]] = if(!i.hasNext) Future successful b.result else Future { b += f(i.next()) } flatMap { b => next(i, b) }
next(s.toIterator, cbf(s))
}
scala> linearize(1 to 100)(_.toString) foreach println
scala> Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
答案 3 :(得分:2)
如果您有一些值的序列,并且想要将它们映射到 private void playPauseClicked()
{
Status currentStatus = player.getStatus();
if(currentStatus == Status.PLAYING)
{
Duration d1 = player.getCurrentTime(); //To measure the difference
player.pause();
Duration d2 = player.getCurrentTime();
VIDEO_PAUSED = true;
}
else if(currentStatus == Status.PAUSED || currentStatus == Status.STOPPED)
{
player.play();
VIDEO_PAUSED = false;
}
}
并按顺序执行它们:
Future
答案 4 :(得分:0)
或者您可以使用{..} yield block来获取Future的序列。 Future.sequence将Seq [Future]转换为Future [Seq]
答案 5 :(得分:-1)
此代码将允许您按顺序阻止https://stackoverflow.com/a/41657239
执行期货