|
Groovy example source code file (SynchronizedBytecodeBug.groovy)
The Groovy SynchronizedBytecodeBug.groovy source code/* * Copyright 2003-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package groovy.bugs /** * @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) } // GROOVY-4159 void testExceptionInSynchronized() { def obj = 1 try { synchronized(obj) { obj.e } assert false } catch (MissingPropertyException e) { assert true } checkNotHavingAMonitor(obj) } 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 } } } Other Groovy examples (source code examples)Here is a short list of links related to this Groovy SynchronizedBytecodeBug.groovy source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.