// This file defines 4 classes... class Thing { private int x; private int y; public Thing() { x = 0; y = 0; } public int getX() { return x; } public int getY() { return y; } public void setX(int i) { x = i; } public void setY(int i) { y = i; } public String toString() { return "(" + x + ", " + y + ")"; } } //================================================================ class ModifyX implements Runnable { private Thing t; // Initialize the object we want to modify later in run() public ModifyX(Thing t) { this.t = t; } public void run() { // Let's increment the value of x for (int i = 0; i < 50000000; ++i) { t.setX(t.getX() + 1); } } } //============================================================== class ModifyY implements Runnable { private Thing t; // Initialize the object we want to modify later in run() public ModifyY(Thing t) { this.t = t; } public void run() { // Let's increment the value of y more slowly for (int i = 0; i < 50000000; ++i) { t.setY(t.getY() + 1); int wasteOfTime = t.getX() + t.getY(); } } } //============================================================ public class Driver { public static void main(String [] args) { Thing t = new Thing(); System.out.printf("Initially, t = %s\n", t.toString()); Thread w1 = new Thread(new ModifyX(t)); Thread w2 = new Thread(new ModifyY(t)); w1.start(); w2.start(); /* try { w1.join(); } catch (InterruptedException e) { } */ for (int i = 0; i < 100; ++i) { System.out.printf("t = %s\n", t.toString()); for (int j = 0; j < 500000; ++j) { int wasteOfTime = t.getX(); } } } }