Get your CodeRanch badge!*
The moose likes Servlets and the fly likes Thread and Heap Memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread and Heap Memory" Watch "Thread and Heap Memory" New topic
Author

Thread and Heap Memory

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
When many request access the servlet then it will be multi threaded,means each thread will execute the servlets’ service method(EX:dopost or doget) in its own stack

Ex:



}
1) all objects are in Heap memory. Even IF a object is created inside a method ,it will exit in heap memory.
2) In the above code Variable “a” is create a new instance of class “A” and keep it in Heap Memory.
3) For example if Three requests access the servlet abc ,means three Threads execute the dopost method. so that It create three separate Instance of Class “A”.
4) So in application, every thing is written servlets’ service method only.

so everything is Thread-safe.??

thne
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi,

From my understanding, the code that you posted should be thread-safe. The variable is just a local variable. But when you change it to



now its not thread-safe.

To say in other words, local variables are thread safe, while instance variables are not.
Also context-scoped attributes and session-scoped attributes would not be thread safe as such.


Blog
John Kimball
Ranch Hand

Joined: Apr 13, 2009
Posts: 96
As far as I know, as long as you don't store the instance anywhere (e.g., session.putValue...) you're ok.

That is, there's no way to reference the instance outside of the method AND the object's lifecycle is only for the lifetime of the method call.

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
John Kimball wrote:As far as I know, as long as you don't store the instance anywhere (e.g., session.putValue...) you're ok.


Not really. Just consider this case



Here we don't store counter's reference anywhere else but we are still in trouble.

Imagine ThreadOne has executed LineA and has gone into RegionX
Now ThreadTwo comes in and executes LineA.
So the counter is now 2.
ThreadOne gets the control back and executes LineB. When what was expected is a value 1, what is obtained is 2.

Thus instance variables tend to be thread-unsafe.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Vishwanath Murthi wrote:Not really. Just consider this case

Your case is not the same. John was assuming a local variable, not an instance variable.

Thus instance variables tend to be thread-unsafe.

No "tend" about it. Instance variables are not thread-safe.
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
IN a Web application every thing inside dopost or doget method only.So the entire application is thread safe,then when to use "syncronized" Statement
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Bear Bibeault wrote:
Vishwanath Murthi wrote:Not really. Just consider this case

Your case is not the same. John was assuming a local variable, not an instance variable.


Oops!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

jacob deiter wrote:IN a Web application every thing inside dopost or doget method only.So the entire application is thread safe,then when to use "syncronized" Statement

I have never had an occasion to use synchronized.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39544
    
  27
Bear Bibeault wrote:
Thus instance variables tend to be thread-unsafe.

No "tend" about it. Instance variables are not thread-safe.

I'd say they're potentially not thread-safe; whether there is or is not actual thread (un)safety depends on the usage. For example, if an instance variable is populated in a servlet's init method, and not modified later, it's still thread-safe.

Also, as mentioned, it's possible to make instance variables thread-safe by using synchronization or Locks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

It's sort of like a loaded gun. It's always dangerous, but if you don't touch it chance are you'll be safe.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread and Heap Memory
 
Similar Threads
Single Thread Model
instance of a servlet
Servlet synchronization question.....
Which of the following statements is correct regarding the servlets that implement
CGI and servlets