// Sum2.java class SumThread extends Thread { private int from, to, sum; public SumThread(int from, int to) { this.from = from; this.to = to; sum = 0; } @Override public void run() { for (int i = from; i <= to; i++) { sum += i; } } public int getSum() { return sum; } } public class Sum2 { public static void main(String args[]) { SumThread t1 = new SumThread(1, 5000); SumThread t2 = new SumThread(5001, 10000); t1.start(); t2.start(); // wait for the threads to finish! try { t1.join(); t2.join(); } catch (InterruptedException e) { System.out.println("Interrupted"); } System.out.printf("The sum of 1-1000 is %d.\n", t1.getSum() + t2.getSum()); } }