scala - 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
复制代码

 

posted @   快乐的张小凡  阅读(2211)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示