1.1 --- a/samples/deadlock/src/org/apidesign/javamonitorflaws/Cache.java Wed Feb 11 07:12:41 2009 +0100
1.2 +++ b/samples/deadlock/src/org/apidesign/javamonitorflaws/Cache.java Wed Feb 11 08:52:00 2009 +0100
1.3 @@ -12,6 +12,7 @@
1.4 *
1.5 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.6 */
1.7 +// BEGIN: monitor.pitfalls.Cache
1.8 public abstract class Cache<From,To> {
1.9 private Map<From,To> cache;
1.10
1.11 @@ -44,3 +45,4 @@
1.12 }
1.13 }
1.14 }
1.15 +// END: monitor.pitfalls.Cache
2.1 --- a/samples/deadlock/test/org/apidesign/javamonitorflaws/CacheTest.java Wed Feb 11 07:12:41 2009 +0100
2.2 +++ b/samples/deadlock/test/org/apidesign/javamonitorflaws/CacheTest.java Wed Feb 11 08:52:00 2009 +0100
2.3 @@ -98,25 +98,32 @@
2.4 org.apidesign.javamonitorflaws.CacheTest$2ToDeadlock.run:90
2.5 java.lang.Thread.run:619
2.6 */
2.7 + // BEGIN: monitor.pitfalls.block.propertychange
2.8 public void testDeadlockViaAPI() throws Exception {
2.9 if (Boolean.getBoolean("no.failures")) return;
2.10
2.11 class ToDeadlock implements Runnable, PropertyChangeListener {
2.12 + int lastMultiply;
2.13 +
2.14 public void run() {
2.15 cache.setMultiply(10);
2.16 }
2.17 public void propertyChange(PropertyChangeEvent evt) {
2.18 try {
2.19 - Thread.sleep(500);
2.20 + storeMultiply();
2.21 } catch (InterruptedException ex) {
2.22 // ok
2.23 }
2.24 - assertMultiplyByTen();
2.25 }
2.26
2.27 - public synchronized void assertMultiplyByTen() {
2.28 - int value = cache.get("123");
2.29 - assertEquals("3*10=30", 30, value);
2.30 + private synchronized void storeMultiply()
2.31 + throws InterruptedException {
2.32 + lastMultiply = cache.getMultiply();
2.33 + // simulates "starvation"
2.34 + wait();
2.35 + }
2.36 +
2.37 + public void assertMultiplyByTen() {
2.38 }
2.39 }
2.40 ToDeadlock toDeadlock = new ToDeadlock();
2.41 @@ -126,6 +133,10 @@
2.42
2.43 Thread.sleep(100);
2.44
2.45 - toDeadlock.assertMultiplyByTen();
2.46 + // BEGIN: monitor.pitfalls.brokencall
2.47 + int value = cache.get("123");
2.48 + assertEquals("3*10=30", 30, value);
2.49 + // END: monitor.pitfalls.brokencall
2.50 }
2.51 + // END: monitor.pitfalls.block.propertychange
2.52 }
2.53 \ No newline at end of file
3.1 --- a/samples/deadlock/test/org/apidesign/javamonitorflaws/MultiplyCache.java Wed Feb 11 07:12:41 2009 +0100
3.2 +++ b/samples/deadlock/test/org/apidesign/javamonitorflaws/MultiplyCache.java Wed Feb 11 08:52:00 2009 +0100
3.3 @@ -8,6 +8,7 @@
3.4 *
3.5 * @author Jaroslav Tulach <jtulach@netbeans.org>
3.6 */
3.7 +// BEGIN: monitor.pitfalls.subclass
3.8 public class MultiplyCache extends Cache<String,Integer> {
3.9 private PropertyChangeSupport pcs;
3.10 private int multiply;
3.11 @@ -38,6 +39,7 @@
3.12 protected Integer createItem(String f) {
3.13 return f.length() * getMultiply();
3.14 }
3.15 +}
3.16 +// END: monitor.pitfalls.subclass
3.17
3.18
3.19 -}
3.20 \ No newline at end of file