Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance variable

 
kathir chokka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HF SJ in page 201. they discussing about to protect instance variable

i could not able understand what they really meant by instance variable can anyone please tell someone where we will be using it.

thanks.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i could not able understand what they really meant by instance variable can anyone please tell someone where we will be using it.


For understanding on that page , you need to revise the concept of instance variable and concurrency topic in SCJP...

In servlet , instance variable is the non-static variable declare within the Servlet Class and outside other method in the class (probably doPost() or doGet() method ) . For your information : all variable declared within the method is local variable and they are tread-safe.

Since each Servlet will only have one instance (per JVM) after initiated, when more than one client request for this particular servlet reach the Container , the Container will create/allocate a thread for each client request, and thus different thread referring to the same servlet object (so ... this is where thread-safety problem comes in).

In the HFSJ pg 201 as you mentioned, which is a deprecated way to protect the instance variable ...

1) Queue all requests
----------------------
If you queue all requests, only one thread will be accessing the servlet object , the next will only comes into Running when the previous one completed. So no more than one thread will accessing the servlet object's instance variable and causes thread safe problem.

2) Send requests through a pool
--------------------------------
This breack the specification since it create one servlet object for each client request for that servlet (specification: only ONE servlet object per JVM), since each thread (i.e. each client request) accessing its own copy of servlet object, thus no thread-safe problem.
 
kathir chokka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic