Hi,
I have read the following statement in some mock
test to select T/F, and the answer is true.
"Threads operate on their own copies of instance variables not on the originals".
If I run the following code, I am getting the r.j value 2.
class MyRunnable implements Runnable {
int i=0;
static int j=0;
public void run(){
int k=0;
increment(k);
System.out.println(k); //line 1
}
private void increment(int k){i++; j++; k++;}
}
public class Question33{
public static void main(
String[] args) throws Exception{
MyRunnable r = null;
Thread t1 = new Thread(r=new MyRunnable());
Thread t2 = new Thread(r);
t1.start();t2.start();
t2.join();t1.join();
System.out.println(r.i+","+r.j); //line 2
}
}
How does the above statement is true?
Please clarify.