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