This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Raj Chila
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18091
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said above, the implementation of the clone() method for a particular object should be a give away.
 
Ram Prasadh Anandaraj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic