/* * SynchRácer.java * * 1.0 * * 000908 * * Leif Lindbäck. * */ /** * A runnable used to cause a race condition. */ public class SynchRacer implements Runnable { private UpdateMe um; private final int NO_OF_UPDATES = 200000; /** * Constructs a SynchRacer that will update the specified UpdateMe object. */ public SynchRacer( UpdateMe um ) { this.um = um; } /** * Updates um NO_OF_UPDATES times and print the result. */ public void run() { System.out.println( Thread.currentThread() + " starting"); for (int i = NO_OF_UPDATES; i>0; i--) { um.addToValue( 10 ); } for (int i = NO_OF_UPDATES; i>0; i--) { um.addToValue( -10 ); } System.out.println( Thread.currentThread() + " done"); } public static void main( String[] args ) throws InterruptedException { /* Create some threads that will update the same field. */ UpdateMe um = new UpdateMe(); SynchRacer r = new SynchRacer( um ); Thread theThreads[] = new Thread[5]; System.out.println( "before update, value = " + um.getValue() ); for(int i=0; i