This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Threads access to object and performance/object in ServletContext: value or reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Threads access to object and performance/object in ServletContext: value or reference" Watch "Threads access to object and performance/object in ServletContext: value or reference" New topic
Author

Threads access to object and performance/object in ServletContext: value or reference

Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Hello all!
Let's suppose I have an object:

1. What would be the performance issues if the getElem method of this object is accessed statically from many servlet threads (1000s of threads)? (also considering that a hashtable is synchronized)
2. Are attributes objects retrieved from the ServletContext returned by value or is it by reference? (i.e, a large hashtable stored in the ServletContext)
[ March 28, 2004: Message edited by: Brahim Bakayoko ]

SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="http://webtools.servehttp.com" target="_blank" rel="nofollow">http://webtools.servehttp.com</a> down for a while...
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Hi Brahin,
I think the Hashtable itself would have to be declared static ( which you have not, may be its just a typing mistake). I would consider this not a performance issue because the hashtable is functioning like a repository. If you are not using it to put some new Objects into it then I think it would be a performance issue as the other threads will have to wait if you synchronize the Hashtable.
I think all the Objects in the ServletContext are returned by value when you call through the getAttribute() method (otherwise what would happen if another servlet is calling the same Object?).
Hope this info is good enough.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12753
    
    5
1. Compared to all the other time consuming operations that take place in servlets, the extra time consumed would be miniscule.
2. Of course you only get a reference when you retrieve something from a ServletContext. There are no collections in Java that return a copy of an object.
All Threads servicing requests to a given servlet share the same ServletContext - if this causes a problem you need to redesign the application. If you are used to programming single user applications, this change of viewpoint is one of the major mental adjustments you have to make on moving to servlets.
Bill
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Originally posted by RajaniKanth Bhargava:
Hi Brahin,
I think the Hashtable itself would have to be declared static ( which you have not, may be its just a typing mistake).

Oops! sorry for the typo...
If you are not using it to put some new Objects into it then I think it would be a performance issue as the other threads will have to wait if you synchronize the Hashtable.

It think you meant if I am using it to store objects, then it would be a performance issue whenever there is rehashing.
Yes, that's true and I plan to create the Hashtable such that its capacity is greater than its fully loaded size to minimize the potential for rehashing. The Hashtable will be mainly used as a source with occasional updates.
Does the Hashtable lock itself whenever there is an insert like database rows? If yes, then what is the transaction mode? (dirty read? read committed? ...)

I think all the Objects in the ServletContext are returned by value when you call through the getAttribute() method (otherwise what would happen if another servlet is calling the same Object?).
Hope this info is good enough.

[edit]: Well I did a simple test (so much for being lazy) and clearly attributes are returned by reference.
My question came from the fact that Hashtables are passed by reference and that when copied only a shalow copy is made (normally).
[edit]: OK, only collection object attributes are returned by reference, else they are returned by value.
[ March 28, 2004: Message edited by: Brahim Bakayoko ]
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Originally posted by William Brogden:
1. Compared to all the other time consuming operations that take place in servlets, the extra time consumed would be miniscule.

Well I am glad to hear that...

2. Of course you only get a reference when you retrieve something from a ServletContext. There are no collections in Java that return a copy of an object.

Well, the simple test I did shows that attributes are returned:
- by reference if the object is a Hashtable (probably HashMap too)
- by value if it is a Vector or any other object
The trick is to note the behavior of the clone() method of the object in question.
- if shallow copy: then reference
- if deep copy: then value
Thanks for the responses...
[ March 28, 2004: Message edited by: Brahim Bakayoko ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12753
    
    5
Well, the simple test I did shows that attributes are returned:
- by reference if the object is a Hashtable (probably HashMap too)
- by value if it is a Vector or any other object

You are trying to tell me that if a ServletContext contains a reference to a Vector then when you do a get, what it returns is NOT a copy of the reference but a clone?
I am eagerly awaiting your proof of this.
Bill
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15947
    
  19

Actually, I believe that HashTable, like Vector is already synchronized and that no special mechanisms should be attached for multi-threading purposes (at best, it's extra overhead, at worst, a potential source of deadlocks).
The newer Collection classes are not synchronized, but I think all the original ones were, so I'd RTFM.


Customer surveys are for companies who didn't pay proper attention to begin with.
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Originally posted by William Brogden:

You are trying to tell me that if a ServletContext contains a reference to a Vector then when you do a get, what it returns is NOT a copy of the reference but a clone?
I am eagerly awaiting your proof of this.
Bill


Well below is your proof my friend.

Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
As I said above, the implementation of the clone() method for a particular object should be a give away.
Ram Prasadh Anandaraj
Greenhorn

Joined: Feb 19, 2004
Posts: 4
Hi Brahmin I think ur assumption is wrong.. try doing this,
private void initMoboSpecs2(){
Hashtable ht = (Hashtable)ctx.getAttribute("specs_ht");
ht.put(new Integer(5), "newProductName");
ht.put(new Integer(6), "newFormFactor");
ht.put(new Integer(7), "newChipset");
ht.put(new Integer(8), "newProcessorClass");
//ctx.setAttribute("specs_ht", ht);
ctx.setAttribute("value", new Integer(2));
Vector v = (Vector) ctx.getAttribute("v");
v.addElement("hi brahmin");
// ctx.setAttribute("v", new Vector(ht.values()));
}
"hi brahmin" will be reflected in both the iterations. If vector were to be cloned at this point,
Vector v = (Vector) ctx.getAttribute("v");
then "hi brahmin" should not appear in the first iteration of the vector.
prasadh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12753
    
    5
Well, that is strange and marvelous code to be sure but I don't see what it has to do with the question. It is your code that is mucking about with the servlet context, not the servlet context returning a clone. Looking at the source code for
org.apache.catalina.core.ApplicationContext
which implements ServletContext in Tomcat, we find:

No sign of any clone operation there.

Bill
 
jQuery in Action, 2nd edition
 
subject: Threads access to object and performance/object in ServletContext: value or reference
 
Similar Threads
singleton class or to have a static variable in a class
singleton class or to ahve a static variable inside a class
Test class
Handle situation where there can be Potential increase in the method parameters.
how to determine the real type of a generic property?