001package observer.three; 002public class Main { 003 public static void main(String[] argv) { 004 Int c = new Int(); 005 Runnable r1 = new M(c); 006 Runnable r2 = new N(c); 007 for (int i=0; i<10000; i++) { 008 r1.run(); 009 r2.run(); 010 } 011 } 012} 013class Int { 014 private int v; 015 private int numOps; 016 public int get() { return v; } 017 public void inc() { v++; System.out.println (++numOps); } 018 public void dec() { v--; System.out.println (++numOps); } 019} 020class M implements Runnable { 021 private Int c; 022 public M(Int c) { this.c = c; } 023 public void run() { 024 c.inc(); 025 c.inc(); 026 c.dec(); 027 } 028} 029class N implements Runnable { 030 private Int c; 031 public N(Int c) { this.c = c; } 032 public void run() { 033 for (int i=0; i<50; i++) { 034 if (i%3==0) { 035 c.dec(); 036 } else { 037 c.inc(); 038 } 039 } 040 } 041}