Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and Objects

 
manjit singh thakurratan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am a bit confused on how two threads access a single object

Assume the following

class abc{
public void printer(){
int a =0;
int b=0;

a++;
b++;

System.out.println(a + " this is a and b is " + b );
}

}

Now assuming two threads access the method printer(). I am confused how the local variables are managed..

In the sense do the two threads have their stack variables in the sense that do they have their very own vairable a and variable b?
Or
Do they share a common set of local variables in the printer method ?

Regards
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Suppose you have two threads which do that run() method. Each thread is a new stack - each invocation will be on thread's stack.
So I think you can answer your question.
 
manjit singh thakurratan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So ..

the object is the same... but 2 threads are invoking the same method....

so 2 sets of method local variables....

and the instance variables will be shared!
 
Anil Gautam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
each thread keeps separate copy of local variables in a stack, they never shared.
 
manjit singh thakurratan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ..

So the thing that happens in webservers is like

when a request comes... in ... a new thread is started.. and then ..a new set of local variables get created...

oh ..
so when we want to share info between requests we can use instance variables!

cooll.... i got my answer!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!