class Counter { int x = 0, y = 0; public void increment() { // if (x != y) { // System.out.println("x = " + x + "; y = " + y); // x = y = 0; // } x++; y++; } } class Worker implements Runnable { private Counter counter; public Worker (Counter c) { counter = c; } public void run() { for(int i = 0; i < 500000000; i++) counter.increment(); } } public class counter { public static final void main (String[] args) { Counter c1 = new Counter(); Worker w1 = new Worker(c1); w1.run(); Counter c2 = new Counter(); Worker w2 = new Worker(c2); w2.run(); System.out.println("c1: x = " + c1.x + "; y = " + c1.y); System.out.println("c2: x = " + c2.x + "; y = " + c2.y); } // public static final void main (String[] args) { // Counter c1 = new Counter(); // Worker w1 = new Worker(c1); // Thread t1 = new Thread(w1); // t1.start(); // Counter c2 = new Counter(); // Worker w2 = new Worker(c2); // Thread t2 = new Thread(w2); // t2.start(); // try { // t1.join(); // t2.join(); // } catch (InterruptedException x) { // } // System.out.println("c1: x = " + c1.x + "; y = " + c1.y); // System.out.println("c2: x = " + c2.x + "; y = " + c2.y); // } }