1 package org.apidesign.deadlock;
4 import static org.junit.Assert.*;
6 public class SynchronizedFieldsTest {
7 public SynchronizedFieldsTest() {
11 public void increment() {
12 SynchronizedFields instance = new SynchronizedFields();
17 public void unsafeDecrement() {
18 SynchronizedFields instance = new SynchronizedFields();
20 instance.unsafeDecrement();
21 } catch (java.lang.AssertionError ex) {
25 fail("This will fail as unsafeDecrement is not synchronized, and that" +
26 "is why it cannot access the field using getter and setter"
31 public void fixUnsafeDecrementFromOutside() {
32 SynchronizedFields instance = new SynchronizedFields();
33 synchronized (instance) {
34 // in contract to original "Monitors", one can "fix" this
35 // problem from outside by using synchronizing externally
36 instance.unsafeDecrement();