gpt4 book ai didi

并发程序中的 Scala 模式匹配

转载 作者:行者123 更新时间:2023-12-04 18:47:26 26 4
gpt4 key购买 nike

我是 Scala 的新手,我想编写一些带有模式匹配的多线程代码,我想知道我是否可以将模式匹配代码视为原子代码。

例如:

abstract class MyPoint
case class OneDim(x : Int) extends MyPoint
case class TwoDim(x : Int, y : Int) extends MyPoint

var global_point : MyPoint = new OneDim(7)

spawn {
Thread.sleep(scala.util.Random.nextInt(100))
global_point = new TwoDim(3, 9)
}
Thread.sleep(scala.util.Random.nextInt(100))

match global_point {
case TwoDim(_, _) => println("Two Dim")
case OneDim(_) => println("One Dim")
}

是否有可能执行如下:
  • 主线程到达“匹配 global_point”代码,发现 *global_point* 不是 TwoDim 类型并暂停(返回到调度程序)。
  • 生成的线程将 *global_point* 更改为 TwoDim
  • 类型
  • 主线程返回,发现 *global_point* 不是 OneDim 类型,认为没有匹配到 *global_point* 并引发 NoMatch 异常。

  • Scala是否在内部避免了这种执行?如果是这样,那怎么办?匹配是否拍摄对象的快照,然后尝试将其与模式匹配?快照深度是否有限制(匹配模式可能很复杂和嵌套)?

    最佳答案

    这不是规范中的确凿证据,但它说明了编译器为您所做的一些事情,这应该允许您考虑 几个将 block 匹配为原子 - 但绝对不是全部 .如果您自己同步代码,或者使用不可变对象(immutable对象),这会更安全。
    平面示例
    如果您使用 scala -print 运行以下脚本:

    var m: Option[String] = _

    m match {
    case Some(s) => "Some: " + s
    case None => "None"
    }
    您将看到编译器创建的脱糖中间代码(为简洁起见,我删除了一些代码):
    final class Main$$anon$1 extends java.lang.Object {
    private[this] var m: Option = _;

    private <accessor> def m(): Option = Main$$anon$1.this.m;

    def this(): anonymous class Main$$anon$1 = {
    <synthetic> val temp1: Option = Main$$anon$1.this.m();

    if (temp1.$isInstanceOf[Some]()) {
    "Some: ".+(temp1.$asInstanceOf[Some]().x())
    else if (scala.this.None.==(temp1))
    "None"
    else
    throw new MatchError(temp1)
    }
    }
    m 引用的可能共享对象获取本地别名 temp1 , 所以如果 m在背景中更改,使其指向另一个对象,然后匹配仍然发生在旧对象 m指向。因此,您在上面描述的情况(将 global_point 更改为指向 TwoDim 而不是 OneDim )不是问题。
    嵌套示例
    通常情况下,编译器似乎会为所有绑定(bind)在匹配案例保护中的对象创建本地别名,但它不会创建深拷贝!
    对于以下脚本:
    case class X(var f: Int, var x: X)

    var x = new X(-1, new X(1, null))

    x match {
    case X(f, ix) if f > 0 || ix.f > 0 => "gt0"
    case X(f, ix) if f <= 0 || ix.f <= 0 => "lte0"
    }
    编译器创建这个中间代码:
    private[this] var x: anonymous class Main$$anon$1$X = _;

    private <accessor> def x(): anonymous class Main$$anon$1$X = Main$$anon$1.this.x;

    final <synthetic> private[this] def gd2$1(x$1: Int, x$2: anonymous class Main$$anon$1$X): Boolean = x$1.>(0).||(x$2.f().>(0));

    final <synthetic> private[this] def gd3$1(x$1: Int, x$2: anonymous class Main$$anon$1$X): Boolean = x$1.<=(0).||(x$2.f().<=(0));

    def this(): anonymous class Main$$anon$1 = {
    <synthetic> val temp6: anonymous class Main$$anon$1$X = Main$$anon$1.this.x();

    if (temp6.ne(null)) {
    <synthetic> val temp7: Int = temp6.f();
    <synthetic> val temp8: anonymous class Main$$anon$1$X = temp6.x();

    if (Main$$anon$1.this.gd2$1(temp7, temp8))
    "gt0"
    else if (Main$$anon$1.this.gd3$1(temp7, temp8))
    "lte0"
    else
    throw new MatchError(temp6)
    } else
    throw new MatchError(temp6)
    }
    在这里,编译器为对象 x 创建本地别名。你匹配上,并为其两个子对象 x.f (绑定(bind)到 f )和 x.x (绑定(bind)到 ix ),但不适用于 ix.f .因此,如果您匹配的结构是深度嵌套的,并且您的案例依赖于您未在本地绑定(bind)的嵌套对象,则可能会发生竞争条件。众所周知,由于墨菲定律,并且会。

    关于并发程序中的 Scala 模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11945318/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com