Here is the code I have:
/* User: koray@tugay.biz Date: 21/02/15 Time: 19:53 */
public class DriverClass {
public static void main(String[] args) throws InterruptedException {
int upper = 15;
Integer sumValue = null;
Thread thread = new Thread(new Summation(upper, sumValue));
thread.start();
thread.join();
System.out.println(sumValue);
}
}
and the Summation class:
class Summation implements Runnable {
private int upper;
private Integer sumValue;
public Summation(int upper, Integer sumValue) {
this.upper = upper;
this.sumValue = sumValue;
}
public void run() {
System.out.println("Thread started...");
int sum = 0;
for (int i = 0; i <= upper; i++) {
sum += i;
}
System.out.println("Sum is:" + sum);
sumValue = sum;
}
}
I will see in the console:
Thread started...
Sum is:120
null
Why the last line is 'null' ? I am expecting to see 120 again?