Scala并发编程实战:Monitor与synchronized

Java并发编程最常用和易用的技术莫过于synchronized关键字,而Scala的并发编程之旅也可以从synchronized开始。而synchronized的背后其实是monitor技术。

什么是Monitor

Monitor是解决并发编程问题的一种常用技术,可以有效解决互斥和同步两大常见问题,通常翻译为‘监视器’或‘管程’。个人认为‘管程‘更能表达monitor的含义,指的是管理共享变量以及对共享变量的操作过程,让他们支持并发。

Scala的synchronized

Synchronized是Java对monitor的实现,可以对代码块或方法使用,使得每次只能有一个线程访问,实现了线程互斥。当一个线程获取了锁,其他线程将在队列上等待,实现了线程同步。

Scala延用了这一关键字,但是语法有所不同。

//用于代码块
obj.synchronized {
    ...
}
//用于方法
def func(): Unit = this.synchronized {
    ...
}

跟Java一样,这里的this是可以省略的,因为默认加锁的对象就是this,但是不建议省略。

Scala实例

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

解析

  • 在object SynchronizedDemo和class SynchronizedDemo中均定义了一个inc变量和一个addOne方法,addOne方法的作用就是将inc加1。
  • main方法中,分别创建10个线程调用addOne方法,对inc进行10次加1操作。
  • 因为inc变量不是线程安全的,所以对addOne方法加上synchronized关键字,使得修改操作是线程安全的。这样才能保证inc会从1加到10。
  • object和class中的this并不相同,object中的this指向的是名为SynchronizedDemo的object对象,class中的则是该class实例化后的对象。(Scala中没有静态类和静态方法,object SynchronizedDemo实际上是创建名为SynchronizedDemo的单例对象)

如果把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

本文代码

Github仓库

转载请注明原文地址:https://liam-blog.ml/2019/07/14/Scala-Concurrency-in-Practice-1/

查看更多博主文章

你可能感兴趣的