class Counter implements Runnable { private int counter = 0; private final int rounds = 100000; public void run () { try { int id; String name = Thread.currentThread().getName(); if (name.equals("thread1")) id = 1; else id = 2; for(int i = 0; i < rounds; i++) { // Thread 1 performed rounds - 1 iterations. // Now wait for thread 2 to write 1 to the counter // and read it in our last iteration. if (id == 1 && i == rounds - 1) Thread.sleep (100); int tmp = counter; if (i == 0) Thread.sleep (10); // Let both threads read 0. // Wait for thread 1 to perform rounds - 1 iterations. if (id == 2 && i == 0) Thread.sleep (50); // We read 1 written by thread 2. Now we sleep to allow // thread 2 to finish. if (id == 1 && i == rounds - 1) Thread.sleep (100); counter = tmp + 1; // We wrote 1 to the counter. Now we wait for thread 1 // to read it. if (id == 2 && i == 0) Thread.sleep (100); } } catch (InterruptedException e) { System.out.println ("Interrupted!"); } } public static void main (String[] args) { try { Counter c = new Counter (); // Create two threads that run our run () method. Thread t1 = new Thread (c, "thread1"); Thread t2 = new Thread (c, "thread2"); t1.start (); t2.start (); // Wait for the threads to finish. t1.join (); t2.join (); // Print the counter System.out.println(c.counter); } catch (InterruptedException e) { System.out.println ("Interrupted!"); } } }