看到有这样一个关于并发的小题目,花了2分钟写一个demo
启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75. 程序的输出结果应该为
package com.tiger
import java.util.concurrent.atomic.AtomicInteger
import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props}
object Print{
val count = new AtomicInteger(0)
}
class Print extends Actor with ActorLogging{
override def receive = {
case (v1:ActorRef,v2:ActorRef) =>
for(i <- 1 to 5){
log.debug("{}",Print.count.incrementAndGet()
//睡眠单纯为了适应线程调度
TimeUnit.MILLISECONDS.sleep(100)
}
if(Print.count.get()<75){
v1 ! (v2,self)
}
}
}
object T1 {
def main(args: Array[String]): Unit = {
val system = ActorSystem.create()
val v1 = system.actorOf(Props[Print].withDispatcher("print-dispatcher"))
val v2 = system.actorOf(Props[Print].withDispatcher("print-dispatcher"))
val v3 = system.actorOf(Props[Print].withDispatcher("print-dispatcher"))
v1 ! (v2,v3)
}
}
application.conf
print-dispatcher {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
fixed-pool-size = 3
}
throughput = 100
}
结果:
[DEBUG] [09/05/2017 23:36:06.916] [main] [EventStream] StandardOutLogger started
[DEBUG] [09/05/2017 23:36:07.300] [main] [EventStream(akka://default)] logger log1-Logging$DefaultLogger started
[DEBUG] [09/05/2017 23:36:07.300] [main] [EventStream(akka://default)] logger log1-Logging$DefaultLogger started
[DEBUG] [09/05/2017 23:36:07.302] [main] [EventStream(akka://default)] Default Loggers started
[DEBUG] [09/05/2017 23:36:07.302] [main] [EventStream(akka://default)] Default Loggers started
[DEBUG] [09/05/2017 23:36:07.343] [default-print-dispatcher-9] [akka://default/user/$a] 1
[DEBUG] [09/05/2017 23:36:07.444] [default-print-dispatcher-9] [akka://default/user/$a] 2
[DEBUG] [09/05/2017 23:36:07.545] [default-print-dispatcher-9] [akka://default/user/$a] 3
[DEBUG] [09/05/2017 23:36:07.645] [default-print-dispatcher-9] [akka://default/user/$a] 4
[DEBUG] [09/05/2017 23:36:07.745] [default-print-dispatcher-9] [akka://default/user/$a] 5
[DEBUG] [09/05/2017 23:36:07.846] [default-print-dispatcher-11] [akka://default/user/$b] 6
[DEBUG] [09/05/2017 23:36:07.946] [default-print-dispatcher-11] [akka://default/user/$b] 7
[DEBUG] [09/05/2017 23:36:08.046] [default-print-dispatcher-11] [akka://default/user/$b] 8
[DEBUG] [09/05/2017 23:36:08.146] [default-print-dispatcher-11] [akka://default/user/$b] 9
[DEBUG] [09/05/2017 23:36:08.247] [default-print-dispatcher-11] [akka://default/user/$b] 10
[DEBUG] [09/05/2017 23:36:08.347] [default-print-dispatcher-10] [akka://default/user/$c] 11
[DEBUG] [09/05/2017 23:36:08.447] [default-print-dispatcher-10] [akka://default/user/$c] 12
[DEBUG] [09/05/2017 23:36:08.547] [default-print-dispatcher-10] [akka://default/user/$c] 13
[DEBUG] [09/05/2017 23:36:08.648] [default-print-dispatcher-10] [akka://default/user/$c] 14
[DEBUG] [09/05/2017 23:36:08.748] [default-print-dispatcher-10] [akka://default/user/$c] 15
[DEBUG] [09/05/2017 23:36:08.848] [default-print-dispatcher-9] [akka://default/user/$a] 16
[DEBUG] [09/05/2017 23:36:08.948] [default-print-dispatcher-9] [akka://default/user/$a] 17
[DEBUG] [09/05/2017 23:36:09.049] [default-print-dispatcher-9] [akka://default/user/$a] 18
[DEBUG] [09/05/2017 23:36:09.149] [default-print-dispatcher-9] [akka://default/user/$a] 19
[DEBUG] [09/05/2017 23:36:09.249] [default-print-dispatcher-9] [akka://default/user/$a] 20
[DEBUG] [09/05/2017 23:36:09.350] [default-print-dispatcher-11] [akka://default/user/$b] 21
[DEBUG] [09/05/2017 23:36:09.450] [default-print-dispatcher-11] [akka://default/user/$b] 22
[DEBUG] [09/05/2017 23:36:09.550] [default-print-dispatcher-11] [akka://default/user/$b] 23
[DEBUG] [09/05/2017 23:36:09.650] [default-print-dispatcher-11] [akka://default/user/$b] 24
[DEBUG] [09/05/2017 23:36:09.751] [default-print-dispatcher-11] [akka://default/user/$b] 25
[DEBUG] [09/05/2017 23:36:09.851] [default-print-dispatcher-10] [akka://default/user/$c] 26
[DEBUG] [09/05/2017 23:36:09.951] [default-print-dispatcher-10] [akka://default/user/$c] 27
[DEBUG] [09/05/2017 23:36:10.051] [default-print-dispatcher-10] [akka://default/user/$c] 28
[DEBUG] [09/05/2017 23:36:10.152] [default-print-dispatcher-10] [akka://default/user/$c] 29
[DEBUG] [09/05/2017 23:36:10.252] [default-print-dispatcher-10] [akka://default/user/$c] 30
[DEBUG] [09/05/2017 23:36:10.352] [default-print-dispatcher-9] [akka://default/user/$a] 31
[DEBUG] [09/05/2017 23:36:10.452] [default-print-dispatcher-9] [akka://default/user/$a] 32
[DEBUG] [09/05/2017 23:36:10.553] [default-print-dispatcher-9] [akka://default/user/$a] 33
[DEBUG] [09/05/2017 23:36:10.653] [default-print-dispatcher-9] [akka://default/user/$a] 34
[DEBUG] [09/05/2017 23:36:10.753] [default-print-dispatcher-9] [akka://default/user/$a] 35
[DEBUG] [09/05/2017 23:36:10.854] [default-print-dispatcher-11] [akka://default/user/$b] 36
[DEBUG] [09/05/2017 23:36:10.954] [default-print-dispatcher-11] [akka://default/user/$b] 37
[DEBUG] [09/05/2017 23:36:11.054] [default-print-dispatcher-11] [akka://default/user/$b] 38
[DEBUG] [09/05/2017 23:36:11.154] [default-print-dispatcher-11] [akka://default/user/$b] 39
[DEBUG] [09/05/2017 23:36:11.255] [default-print-dispatcher-11] [akka://default/user/$b] 40
[DEBUG] [09/05/2017 23:36:11.355] [default-print-dispatcher-10] [akka://default/user/$c] 41
[DEBUG] [09/05/2017 23:36:11.455] [default-print-dispatcher-10] [akka://default/user/$c] 42
[DEBUG] [09/05/2017 23:36:11.555] [default-print-dispatcher-10] [akka://default/user/$c] 43
[DEBUG] [09/05/2017 23:36:11.656] [default-print-dispatcher-10] [akka://default/user/$c] 44
[DEBUG] [09/05/2017 23:36:11.756] [default-print-dispatcher-10] [akka://default/user/$c] 45
[DEBUG] [09/05/2017 23:36:11.856] [default-print-dispatcher-9] [akka://default/user/$a] 46
[DEBUG] [09/05/2017 23:36:11.956] [default-print-dispatcher-9] [akka://default/user/$a] 47
[DEBUG] [09/05/2017 23:36:12.057] [default-print-dispatcher-9] [akka://default/user/$a] 48
[DEBUG] [09/05/2017 23:36:12.157] [default-print-dispatcher-9] [akka://default/user/$a] 49
[DEBUG] [09/05/2017 23:36:12.257] [default-print-dispatcher-9] [akka://default/user/$a] 50
[DEBUG] [09/05/2017 23:36:12.358] [default-print-dispatcher-11] [akka://default/user/$b] 51
[DEBUG] [09/05/2017 23:36:12.458] [default-print-dispatcher-11] [akka://default/user/$b] 52
[DEBUG] [09/05/2017 23:36:12.558] [default-print-dispatcher-11] [akka://default/user/$b] 53
[DEBUG] [09/05/2017 23:36:12.658] [default-print-dispatcher-11] [akka://default/user/$b] 54
[DEBUG] [09/05/2017 23:36:12.759] [default-print-dispatcher-11] [akka://default/user/$b] 55
[DEBUG] [09/05/2017 23:36:12.859] [default-print-dispatcher-10] [akka://default/user/$c] 56
[DEBUG] [09/05/2017 23:36:12.959] [default-print-dispatcher-10] [akka://default/user/$c] 57
[DEBUG] [09/05/2017 23:36:13.059] [default-print-dispatcher-10] [akka://default/user/$c] 58
[DEBUG] [09/05/2017 23:36:13.160] [default-print-dispatcher-10] [akka://default/user/$c] 59
[DEBUG] [09/05/2017 23:36:13.260] [default-print-dispatcher-10] [akka://default/user/$c] 60
[DEBUG] [09/05/2017 23:36:13.360] [default-print-dispatcher-9] [akka://default/user/$a] 61
[DEBUG] [09/05/2017 23:36:13.460] [default-print-dispatcher-9] [akka://default/user/$a] 62
[DEBUG] [09/05/2017 23:36:13.561] [default-print-dispatcher-9] [akka://default/user/$a] 63
[DEBUG] [09/05/2017 23:36:13.661] [default-print-dispatcher-9] [akka://default/user/$a] 64
[DEBUG] [09/05/2017 23:36:13.761] [default-print-dispatcher-9] [akka://default/user/$a] 65
[DEBUG] [09/05/2017 23:36:13.862] [default-print-dispatcher-11] [akka://default/user/$b] 66
[DEBUG] [09/05/2017 23:36:13.962] [default-print-dispatcher-11] [akka://default/user/$b] 67
[DEBUG] [09/05/2017 23:36:14.062] [default-print-dispatcher-11] [akka://default/user/$b] 68
[DEBUG] [09/05/2017 23:36:14.162] [default-print-dispatcher-11] [akka://default/user/$b] 69
[DEBUG] [09/05/2017 23:36:14.263] [default-print-dispatcher-11] [akka://default/user/$b] 70
[DEBUG] [09/05/2017 23:36:14.363] [default-print-dispatcher-10] [akka://default/user/$c] 71
[DEBUG] [09/05/2017 23:36:14.463] [default-print-dispatcher-10] [akka://default/user/$c] 72
[DEBUG] [09/05/2017 23:36:14.563] [default-print-dispatcher-10] [akka://default/user/$c] 73
[DEBUG] [09/05/2017 23:36:14.664] [default-print-dispatcher-10] [akka://default/user/$c] 74
[DEBUG] [09/05/2017 23:36:14.764] [default-print-dispatcher-10] [akka://default/user/$c] 75
从更高的角度看并发,将业务模型与底层系统分离,这或许是微服务的一个思路吧
附源码