【问题标题】:How to build a reinitializable lazy property in Groovy?如何在 Groovy 中构建可重新初始化的惰性属性?
【发布时间】:2013-03-15 09:57:46
【问题描述】:

这就是我想做的:

class MyObject {
    @Lazy volatile String test =  {
        //initalize with network access
    }()
}

def my = new MyObject()
println my.test

//Should clear the property but throws groovy.lang.ReadOnlyPropertyException
my.test = null 

//Should invoke a new initialization
println my.test

不幸的是,惰性字段在 Groovy 中是只读字段,清除该属性会导致异常。

知道如何在不重新实现 @Lazy 注释提供的双重检查逻辑的情况下使惰性字段可重新初始化吗?

更新:

考虑到 soft=true(来自第一个答案)让我进行了一些测试:

class MyObject {
    @Lazy() volatile String test =  {
        //initalize with network access
        println 'init'
        Thread.sleep(1000)
        'test'
    }()
}

def my = new MyObject()
//my.test = null 
10.times { zahl ->
    Thread.start {println "$zahl: $my.test"}
}

大约 1 秒后,我的 Groovy 控制台上会显示以下输出:

init
0: test
7: test
6: test
1: test
8: test
4: test
9: test
3: test
5: test
2: test

这是预期的(和想要的)。现在我添加soft=true,结果变化很大,需要10秒:

init
init
0: test
init
9: test
init
8: test
init
7: test
init
6: test
init
5: test
init
4: test
init
3: test
init
2: test
1: test

也许我做错了测试或者 soft=true 完全破坏了缓存效果。有什么想法吗?

【问题讨论】:

  • 认为这可能是soft=true 中的一个错误,我已经更新了我的答案,并将进行更多测试...

标签: groovy lazy-evaluation


【解决方案1】:

你不能用softattribute of Lazy,即:

class MyObject {
  @Lazy( soft=true ) volatile String test =  {
    //initalize with network access
  }()
}

编辑

使用soft=true,注解会生成一个 setter 和一个 getter,如下所示:

private volatile java.lang.ref.SoftReference $test 

public java.lang.String getTest() {
    java.lang.String res = $test?.get()
    if ( res != null) {
        return res 
    } else {
        synchronized ( this ) {
            if ( res != null) {
                return res 
            } else {
                res = { 
                }.call()
                $test = new java.lang.ref.SoftReference( res )
                return res 
            }
        }
    }
}

public void setTest(java.lang.String value) {
    if ( value != null) {
        $test = new java.lang.ref.SoftReference( value )
    } else {
        $test = null
    }
}

没有soft=true,你就没有setter

private volatile java.lang.String $test 

public java.lang.String getTest() {
    java.lang.Object $test_local = $test 
    if ( $test_local != null) {
        return $test_local 
    } else {
        synchronized ( this ) {
            if ( $test != null) {
                return $test 
            } else {
                return $test = { 
                }.call()
            }
        }
    }
}

所以变量是只读的。目前不确定这是故意的,还是使用 soft=true 的副作用...

编辑#2

这看起来可能是使用 soft=true 实现 Lazy 时的错误

如果我们将 getter 更改为:

  public java.lang.String getTest() {
    java.lang.String res = $test?.get()
    if( res != null ) {
      return res 
    } else {
      synchronized( this ) {
        // Get the reference again rather than just check the existing res
        res = $test?.get()
        if( res != null ) {
          return res
        } else {
          res = { 
            println 'init'
            Thread.sleep(1000)
            'test'
          }.call()
          $test = new java.lang.ref.SoftReference<String>( res )
          return res 
        }
      }
    }
  }

我认为它正在工作......我会修复错误

【讨论】:

  • 您的建议似乎有效。不过,我想知道为什么,因为文档说 soft=truemakes test 是一个软引用(据我了解,它可以考虑用于 GC)。那么属性的可写性只是一个副作用吗?
  • 刚刚注意到:soft=true 似乎消除了volatile 的影响(带有双重检查锁定的线程安全)
  • 蒂姆,我在阅读您更新的答案时尝试的是编写我自己的设置器,它在内部使用 $test。这是一个 hack,需要内部知识,但到目前为止它有效。
猜你喜欢
  • 2021-11-21
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多