Help explain phenomenon 2 for the future scala (shown in bold in code4 and code5), thanks.
Code1
package com.tst
import akka.actor.{Actor, ActorSystem, Props}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class MyActor extends Actor {
def receive = {
case _ =>
for (i <- 1 to 6) {
Future {
println("start")
Thread.sleep(30000)
println("end")
}
}
}
}
object Test extends App {
val system = ActorSystem()
val myActor = system.actorOf(Props[MyActor])
myActor ! 'msg
}
For code 1, since the cpu core is 4, so in the first 30 seconds we can just see 4 startprint, no problem for me. (If your processor has more cores, for example 8 cells, you can change the cycle from 6 to 10 to reproduce my question)
Codex2
package com.tst
import akka.actor.{Actor, ActorSystem, Props}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, blocking}
class MyActor extends Actor {
def receive = {
case _ =>
for (i <- 1 to 6) {
Future {
blocking {
println("start")
Thread.sleep(30000)
println("end")
}
}
}
}
}
object Test extends App {
val system = ActorSystem()
val myActor = system.actorOf(Props[MyActor])
myActor ! 'msg
}
For code 2, when added blocking, additional threads were used, so first you can see fingerprint 6 start, no problem for me.
Code3
package com.tst
import akka.actor.{Actor, ActorSystem, Props}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, blocking}
class MyActor extends Actor {
def receive = {
case _ =>
for (i <- 1 to 6) {
Future {
blocking { // lineA
println("startpre")
Thread.sleep(30000)
println("endpre")
}
}
}
Thread.sleep(2000)
for (i <- 1 to 6) {
println("+")
Future {
blocking { // lineB
println("start")
Thread.sleep(30000)
println("end")
}
}
}
}
}
object Test extends App {
val system = ActorSystem()
val myActor = system.actorOf(Props[MyActor])
myActor ! 'msg
}
For code 3, we can see that 6 startpreand 6 startprint in the first 30 seconds, for me there is no problem.
code4
Just remove lineA in code3, output:
startpre
startpre
startpre
startpre
+
+
+
+
+
+
: 4 startpre 30 ? blocking lineB ? , 6 start
code5
lineB code3, , 4, :
startpre
startpre
startpre
startpre
startpre
startpre
+
+
+
+
+
+
start
: 1 start, 4 , lineA Future 2 , 1 1 start