匆匆那些年 2019-11-11
Java并发编程最常用和易用的技术莫过于synchronized关键字,而Scala的并发编程之旅也可以从synchronized开始。而synchronized的背后其实是monitor技术。
Monitor是解决并发编程问题的一种常用技术,可以有效解决互斥和同步两大常见问题,通常翻译为‘监视器’或‘管程’。个人认为‘管程‘更能表达monitor的含义,指的是管理共享变量以及对共享变量的操作过程,让他们支持并发。
Synchronized是Java对monitor的实现,可以对代码块或方法使用,使得每次只能有一个线程访问,实现了线程互斥。当一个线程获取了锁,其他线程将在队列上等待,实现了线程同步。
Scala延用了这一关键字,但是语法有所不同。
//用于代码块 obj.synchronized { ... } //用于方法 def func(): Unit = this.synchronized { ... }
跟Java一样,这里的this是可以省略的,因为默认加锁的对象就是this,但是不建议省略。
import java.util.concurrent.TimeUnit object SynchronizedDemo { private var inc: Int = 0 def addOne(): Unit = this.synchronized { TimeUnit.SECONDS.sleep(1) inc += 1 } def main(args: Array[String]): Unit = { for (i <- 1 to 10) { new Thread { override def run(): Unit = { println(s"run thread with object method $i") addOne() } }.start() } val instance = new SynchronizedDemo for (i <- 1 to 10) { new Thread { override def run(): Unit = { println(s"run thread with class method $i") instance.addOne() } }.start() } while (true) { println(s"object inc=$inc, class inc=${instance.inc}") TimeUnit.SECONDS.sleep(1) } } } class SynchronizedDemo { private var inc: Int = 0 def addOne(): Unit = this.synchronized { TimeUnit.SECONDS.sleep(1) inc += 1 } }
程序输出
run thread with class method 7 run thread with class method 4 run thread with object method 8 run thread with object method 7 run thread with class method 10 run thread with class method 8 run thread with class method 9 run thread with object method 5 run thread with object method 3 run thread with object method 2 run thread with object method 4 run thread with object method 10 run thread with object method 9 run thread with class method 5 run thread with class method 3 object inc=0, class inc=0 run thread with object method 1 run thread with class method 6 run thread with class method 1 run thread with class method 2 run thread with object method 6 object inc=1, class inc=1 object inc=2, class inc=2 object inc=3, class inc=2 object inc=4, class inc=4 object inc=5, class inc=5 object inc=6, class inc=6 object inc=7, class inc=7 object inc=8, class inc=8 object inc=9, class inc=9 object inc=10, class inc=10
解析
如果把class中定义的addOne改成如下:
def addOne(): Unit = SynchronizedDemo.synchronized { TimeUnit.SECONDS.sleep(1) inc += 1 }
两处定义的addOne方法就会互斥,输出就会变成如下:
run thread with object method 2 run thread with object method 1 run thread with object method 3 run thread with object method 4 run thread with object method 5 run thread with object method 6 run thread with object method 7 run thread with object method 8 run thread with object method 9 run thread with object method 10 run thread with class method 1 run thread with class method 2 run thread with class method 3 run thread with class method 4 run thread with class method 5 run thread with class method 6 run thread with class method 7 run thread with class method 8 run thread with class method 9 run thread with class method 10 object inc=0, class inc=0 object inc=1, class inc=0 object inc=1, class inc=1 object inc=1, class inc=2 object inc=1, class inc=3 object inc=1, class inc=4 object inc=1, class inc=5 object inc=1, class inc=6 object inc=1, class inc=7 object inc=1, class inc=8 object inc=1, class inc=9 object inc=1, class inc=10 object inc=2, class inc=10 object inc=3, class inc=10 object inc=4, class inc=10 object inc=5, class inc=10 object inc=6, class inc=10 object inc=7, class inc=10 object inc=8, class inc=10 object inc=9, class inc=10 object inc=10, class inc=10
转载请注明原文地址:https://liam-blog.ml/2019/07/14/Scala-Concurrency-in-Practice-1/