• 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

Threads and Objects

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic