• 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

instance variable

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks..
 
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic