It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Threads" Watch "Regarding Threads" New topic
Author

Regarding Threads

Swathi Sree
Greenhorn

Joined: Aug 27, 2005
Posts: 19
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.
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185

static int j=0;
.....
System.out.println(r.i+","+r.j); //line 2



'j' is defined as static. Then, how is it accessible like 'r.j'? Please verify the code.


Manivannan
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

You can access the static variable with an instance reference though its not the recommended & commonly accepted way..

Class.staticvariable is the preferred way which the earlier post was pointing to..

Here you are running the two thread and passing the same instance of r ...

Here the k is local to the run ...and in the increment method again its only the parameter k local to the method that is getting incremented ...but you are incrementing the instance variable i and class level variable j ...

This happens twice ....for t1 and t2...

So in the run method ....you get 0 printed twice...since the variable k in never modified inside the run...


But the other variables i and j are modified inside..increment method ...

and so you get 2 for them...

Hope you get it...
[ September 30, 2005: Message edited by: A Kumar ]
Abhijit Sontakey
Ranch Hand

Joined: Sep 26, 2005
Posts: 67
Hi,
Threads have their own instance variables unless the variabled are of type volatile. However in the code mentioned by you, the variable j is a static variable. Hence when the two threads have run, the value will definetly be 2. However the value if i might still be 1.

Let me know if it helps.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,


If you create another instance of r ..and execute the program...

You can see the change...

j is still 2 ..since its a static class level variable..


but

i is 1 ...because i has been modified only once for the latest r instance..

i have changed only this line..

Thread t2 = new Thread(r=new MyRunnable());

I will paste the code here..



Run your code and then this modified code and see the output...


Hope you get it
[ September 30, 2005: Message edited by: A Kumar ]
Swathi Sree
Greenhorn

Joined: Aug 27, 2005
Posts: 19
Hi All,

Thank you for the above explanations, But by mistake I have given var j.

My doubt is regarding variable i which is instance variable.
j is static thus, it will be incremented twice.

But i is instance variable and the statement given(before code) is mentioning that different threads will have their local copies of instance variables.

That is where I confused. Please clarify.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

The statement holds good for your code and mine...

In your code ...both the threads are running the same instance of r.

and so the value becomes 2 (increment i once in each thread)

In my code...

i am passing two instances of r...one for each thread..

and you can see the difference ... it prints 1 for the value of i

and j - 2 since it is a static variable..

thereby proving that the statement should be true..

The i value is 1 since it prints the value for the second instance( which is latest value in r)of r..and not the first instance of r.


Hope you get it..

[ September 30, 2005: Message edited by: A Kumar ]
[ September 30, 2005: Message edited by: A Kumar ]
Swathi Sree
Greenhorn

Joined: Aug 27, 2005
Posts: 19
Hi,

I have understood the concept of how the instance variables are changing while using threads(If there exists two threads with same instance of runnable then they operate on the same copy of the variable i.e original value otherwise they operate on different copies of instance variable.).

How come we can give true to this statement "Threads operate on their own copies of instance variables not on the originals".

How does the change of instance variables dependent on threads only? It is also dependent on which instance it is working. Can we say T/F for the above statement? Will real exam go for the statements like this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Threads
 
Similar Threads
join()
Synchronize on int shouldn't be necessairy, should it?
Lock Mechanism Doubt
Threads and Synchronization examples
join() method in Thread doesn't work as expected