如何将函数输出传递给 futures,然后将这些 futures 传递给新函数?
How to pass function output in futures and then those futures to a new function?
我的场景如下:
第一步:x=def sum(a,b)
第 2 步:Thread.sleep(1s)
第三步:y=def subtract(a,b)
第四步:Thread.sleep(2s)
第 5 步:成功完成上述步骤后执行 z = multiple(x,y)
我需要在 Scala 中使用 futures 来实现这个场景。请帮忙。
我试过这段代码,但它不工作。
import scala.util.{Failure, Success}
def sum(a:Int ,b:Int) = a+b
def sub(c:Int, d:Int) = c-d
def mul(e: Int, f: Int) = e*f
val Sum1= Future {sum(2,3); Thread.sleep(1000)}
val SumFinal=Sum1.onComplete({
case Success(result) => println(result)
case Failure(e) => println("failed: " + e)
})
val Subt1 = Future {sub(5,3);Thread.sleep(2000)}
val SubtFinal = Subt1.onComplete({
case Success(result) => result
case Failure(e) => println("failed: " + e)
})
val Mul1= mul(SumFinal,SubtFinal)
println(Mul1)
问题 1:
结果例如Future {sub(5,3);Thread.sleep(2000)}
是Thread.sleep
返回的值,在Scala中是()
。只需更改顺序:Future {Thread.sleep(2000); sub(5,3)}
将在 2 秒后以结果 2
结束。如果你真的想把sleep
放在计算之后,只需将结果存储在一个变量中:
Future {
val res = sub(5,3)
Thread.sleep(2000)
res
}
问题 2:
SumFinal
和 SubtFinal
又是 ()
因为那是 onComplete
returns。相反,您可以组合两个期货(或更多,或修改一个,等等)并获得未来。一种方法是(在解决问题 1 之后)
val Mul1 = Sum1.zipWith(Sum2)(mul)
Mul1.onComplete {
...
}
您的方法存在问题 onComplete
returns unit
。这就是为什么你没有得到任何结果。所以,subFimal 和 sumFinal 什么都没有。
scala> def sum(a: Int, b: Int) = Future { a + b }
sum: (a: Int, b: Int)scala.concurrent.Future[Int]
scala> def sub(a: Int, b: Int) = Future { a - b }
sub: (a: Int, b: Int)scala.concurrent.Future[Int]
scala> def mul(a: Int, b: Int) = Future { a * b }
mul: (a: Int, b: Int)scala.concurrent.Future[Int]
scala> for {
| a <- sum(2,3)
| b <- sub(10, 7)
| c <- mul(a, b)
| } yield c
res0: scala.concurrent.Future[Int] = Future(<not completed>)
scala> res0
res1: scala.concurrent.Future[Int] = Future(Success(15))
我的场景如下:
第一步:x=def sum(a,b)
第 2 步:Thread.sleep(1s)
第三步:y=def subtract(a,b)
第四步:Thread.sleep(2s)
第 5 步:成功完成上述步骤后执行 z = multiple(x,y)
我需要在 Scala 中使用 futures 来实现这个场景。请帮忙。 我试过这段代码,但它不工作。
import scala.util.{Failure, Success}
def sum(a:Int ,b:Int) = a+b
def sub(c:Int, d:Int) = c-d
def mul(e: Int, f: Int) = e*f
val Sum1= Future {sum(2,3); Thread.sleep(1000)}
val SumFinal=Sum1.onComplete({
case Success(result) => println(result)
case Failure(e) => println("failed: " + e)
})
val Subt1 = Future {sub(5,3);Thread.sleep(2000)}
val SubtFinal = Subt1.onComplete({
case Success(result) => result
case Failure(e) => println("failed: " + e)
})
val Mul1= mul(SumFinal,SubtFinal)
println(Mul1)
问题 1:
结果例如Future {sub(5,3);Thread.sleep(2000)}
是Thread.sleep
返回的值,在Scala中是()
。只需更改顺序:Future {Thread.sleep(2000); sub(5,3)}
将在 2 秒后以结果 2
结束。如果你真的想把sleep
放在计算之后,只需将结果存储在一个变量中:
Future {
val res = sub(5,3)
Thread.sleep(2000)
res
}
问题 2:
SumFinal
和 SubtFinal
又是 ()
因为那是 onComplete
returns。相反,您可以组合两个期货(或更多,或修改一个,等等)并获得未来。一种方法是(在解决问题 1 之后)
val Mul1 = Sum1.zipWith(Sum2)(mul)
Mul1.onComplete {
...
}
您的方法存在问题 onComplete
returns unit
。这就是为什么你没有得到任何结果。所以,subFimal 和 sumFinal 什么都没有。
scala> def sum(a: Int, b: Int) = Future { a + b }
sum: (a: Int, b: Int)scala.concurrent.Future[Int]
scala> def sub(a: Int, b: Int) = Future { a - b }
sub: (a: Int, b: Int)scala.concurrent.Future[Int]
scala> def mul(a: Int, b: Int) = Future { a * b }
mul: (a: Int, b: Int)scala.concurrent.Future[Int]
scala> for {
| a <- sum(2,3)
| b <- sub(10, 7)
| c <- mul(a, b)
| } yield c
res0: scala.concurrent.Future[Int] = Future(<not completed>)
scala> res0
res1: scala.concurrent.Future[Int] = Future(Success(15))