/* Based on book example on page 178. */ class Sum { private int sum; public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation(int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i = 1; i <= upper; ++i) sum += i; sumValue.setSum(sum); } } public class Driver { public static void main(String [] args) { if (args.length == 0) { System.err.printf("Need to include upper value as cmd line arg.\n"); System.exit(1); } int upper = Integer.parseInt(args[0]); if (upper < 0) { System.err.printf("Argument cannnot be negative.\n"); System.exit(1); } Sum sumObject = new Sum(); Thread t = new Thread(new Summation(upper, sumObject)); t.start(); try { t.join(); System.out.printf("The sum for %d is %d\n", upper, sumObject.getSum()); } catch (InterruptedException e) { } } }