// Project: Multithreading // Source code file: Main.java // Show how to use threads to add numbers in parallel. // A class that implements Runnable can be run in a thread. public class Main { public static void main(String[] args) throws InterruptedException { // Create three Adder objects to run in parallel. Adder a1 = new Adder(0, 10000); Adder a2 = new Adder(10000, 20000); Adder a3 = new Adder(20000, 30000); Adder a4 = new Adder(30000, 40000); // Create three threads in which to run // the three Adder objects. Thread t1 = new Thread(a1); Thread t2 = new Thread(a2); Thread t3 = new Thread(a3); Thread t4 = new Thread(a4); // Start the threads System.out.println("Start threads"); t1.start( ); t2.start( ); t3.start( ); t4.start( ); // Block until all threads have finished. t1.join( ); t2.join( ); t3.join( ); t4.join( ); System.out.println("All threads finished."); System.out.println("Grand sum: " + (a1.getTotal( ) + a2.getTotal( ) + a3.getTotal( ) + a4.getTotal( ))); } }