gpt4 book ai didi

multithreading - Groovy 是否有用于 block 级同步的注释?

转载 作者:行者123 更新时间:2023-12-03 12:55:10 24 4
gpt4 key购买 nike

我喜欢 @Synchronized Groovy 提供的注解。 It is a safer option比 Java 的 'synchronized' 关键字,因为它使用本地/私有(private)锁变量而不是 'this' 对象。然而,在 Groovy 中,所有规则都可以被打破。 :)

我也知道有 Lombok,一个 Java 库,provides the same feature .

但是有没有办法在方法中使用注释,以便方法的某些代码不同步(以获得更好的性能)而其他部分(出于必要)?

当我输入这个问题时,我可能想出了一个解决我自己问题的方法:

  • 提取方法:创建一个新方法,将需要同步的代码块移动到该方法中。原始代码调用新方法。新方法有@Synchronized。

  • 听起来对吗?有更好的想法吗?

    最佳答案

    But is there a way to use an annotation within a method so that some of the method's code is not synchronized (for better performance) and other parts are (out of necessity)?



    不,那里没有。您描述为一种选择的方法将起作用。

    关于multithreading - Groovy 是否有用于 block 级同步的注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362665/

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