• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Threads

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic