public class Demo extends Thread { private static int count; public static void increment() { count++; } @Override public void run() { for (int i = 0; i < 10; i++) { increment(); } } // ------------------------------------------------------------- public static void main(String args[]) { System.out.println("The counter starts at " + count); // Create, but do not start, 2 threads. Thread thread1 = new Demo(); Thread thread2 = new Demo(); // Start the 2 threads. thread1.start(); thread2.start(); // Now 3 threads are running: main, thread1, thread2. // Wait until both threads have finished. // Note: join can through an exception try { thread1.join(); thread2.join(); } catch (InterruptedException e) { System.out.println("uh oh, unexpected thread failure"); System.exit(1); } // Now thread1 and thread2 have terminated, so it's safe to // access count. System.out.println("The counter is now " + count); } }