synchronizedbytecodebug.groovy

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 111 行

GROOVY
111
字号
/**
 * @author Guillaume Laforge
 */
class SynchronizedBytecodeBug extends GroovyTestCase {

    /**
     * Groovy's bytecode associated with syncrhonized(foo) construct used to generate invalid bytecode
     * This test method shows that the standard wait()/notify() works.
     */
    void testSynchronized() {
        Integer foo = 0

        Thread.start{
            println "sleeping for a moment"
            sleep 1000
            println "slept and synchronizing from thread"
            synchronized(foo) {
                println "notifying"
                foo.notify()
                println "notified"
            }
        }

        println "synchronizing"
        synchronized(foo) {
            println "starting to wait"
            foo.wait()
            println "waited"
        }

        // if this point is reached, the test worked :-)
        assert true
    }
  
  /* more tests to ensure a monitor exit is done at the right place */
    
  void testBreakInSynchronized() {
    Object lock = new Object()
    while (true) {
	    synchronized(lock) {
    		break
    	}
    }
    checkNotHavingAMonitor(lock)
  }
  
  void testContinueInSynchronized() {
    Object lock = new Object()  
    boolean b = true
    while (b) {
	    synchronized(lock) {
	        b = false
    		continue
    	}
    }
    checkNotHavingAMonitor(lock)
  }
  
  void testReturnInSynchronized() {
    Object lock = new Object()  
    methodWithReturn(lock)
    checkNotHavingAMonitor(lock)
  }
  
  def methodWithReturn(lock) {
    synchronized (lock) {
      return
    }
  }
  
  void testBreakInClosureWithSynchronized() {
    Object lock = new Object()
    def c = {
      while (true) {
	      synchronized(lock) {
    	    break
    	  }
      }
      checkNotHavingAMonitor(lock)
    }
    c()
    checkNotHavingAMonitor(lock)
  }
  
  void testContinueInClosureWithSynchronized() {
    Object lock = new Object()
    def c = {
      boolean b = true
      while (b) {
	      synchronized(lock) {
    	    b = false
    	    continue
    	  }
      }
      checkNotHavingAMonitor(lock)
    }
    c()
    checkNotHavingAMonitor(lock)
  }
  
  def checkNotHavingAMonitor(Object lock){
    // if we call notify* or wait without having the
    // monitor, we get an exception.
    try {
      lock.notifyAll()
      assert false,"should have no monitor!"
    } catch (IllegalMonitorStateException imse) {
      assert true
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?