Re: ArrayList vs. Vector
At 05:54 PM 4/23/01, you wrote: So if not, that's not the case? Thanks. Hunter From: Tim O'Neil [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] Date: Mon, 23 Apr 2001 14:26:08 -0700 To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector At 02:07 PM 4/23/2001 -0700, you wrote: But that would only apply to objects kept in the application or session scope, yes? If an object a new object is created and placed in the request scope, it's only going to be accessed by one user (that request) right? Keep thinking that if you write single thread model servlets. Each HTTP request is served by a Thread. Therefore the HttpServletRequest/Response objects are specific to that thread and not accessed by any other (unless you specifically create subthreads in your request processing). The same applies to Objects created in the scope of the request/response--only that thread has access to them. Therefore synchronization of objects created/destroyed within the scope of the request is unnecessary. The single thread servlet model doesn't enter into it, unless you are counting on unsynchronized access to member or static data of the Servlet object, which is used to serve all requests, or other long-lived objects (i.e., don't do that unless you use single thread model, which you really don't want to do.) - Dennis Doubleday email: [EMAIL PROTECTED] yourfit.com, Inc. web: http://www.yourfit.com/
ArrayList vs. Vector
I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
Re: ArrayList vs. Vector
Hunter Hillegas wrote: I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter Vectors are thread safe, by default ArrayLists aren't.
RE: ArrayList vs. Vector
Both containers belong to Java 2 collection hierarchy, and since Web app run under JVM, both are fine... Jerry -Original Message- From: Hunter Hillegas [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 3:37 PM To: Tomcat User List Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
RE: ArrayList vs. Vector
An ArrayList will provide better performance but it is not synchronized. ArrayLists are part of the Java 2 framework. As long as you do not need your application to perform in a pre Java 2 environment it is my opinion that the ArrayList is an attractive option. The methods of dealing with ArrayLists are similar to other data types throughout the collections framework and as such you will have the ability to change your data type in the future with less effort. For instance if you decide that you need a Map or Tree. That is the goal at least. Be aware that it is not synchronized and the programmer must provide their own synchronized methods should they be needed. Regards, Craig Sun Certified Java Programmer -Original Message- From: Hunter Hillegas [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 12:37 PM To: Tomcat User List Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
RE: ArrayList vs. Vector
which (in theory) makes ArrayList's cheaper ... if you dont need synchronized access to your structure, you should use with ArrayList. jb -Original Message- From: Nick Christopher [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 4:59 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector Hunter Hillegas wrote: I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter Vectors are thread safe, by default ArrayLists aren't.
RE: ArrayList vs. Vector
they aren't synchronised, tomcats thread pooling may cause unpredictable numbers of threads to have access to your objects, even once the servlets method has returned(unless you use single thread model). What do you stand to gain at the expense of the risk? -Original Message- From: Hunter Hillegas [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 8:37 PM To: Tomcat User List Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
Re: ArrayList vs. Vector
At 03:59 PM 4/23/2001 -0400, you wrote: Vectors are thread safe, by default ArrayLists aren't. But its a fairly trivial matter use an Collection interface that has a synchronized method to do an operation where synchronizing is desired.
Re: ArrayList vs. Vector
This leads to a new question... What impact does synchronization have on Web applications? Where is it necessary? From: Tim O'Neil [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] Date: Mon, 23 Apr 2001 13:03:25 -0700 To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector At 03:59 PM 4/23/2001 -0400, you wrote: Vectors are thread safe, by default ArrayLists aren't. But its a fairly trivial matter use an Collection interface that has a synchronized method to do an operation where synchronizing is desired.
Re: ArrayList vs. Vector
At 01:09 PM 4/23/2001 -0700, you wrote: This leads to a new question... What impact does synchronization have on Web applications? Where is it necessary? Well, the only good use of a collection is for keeping tabs on a group of data records, right? Well, what happens if one web user hits your baseball averages server and updates Cardinal Albert Pujols batting average while another user tries to update at exactly the same time? This is what synchronization is for.
RE: ArrayList vs. Vector
Your statement is assuming quite a bit about the author's intent to use the object. -Original Message- From: Danny Angus [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 1:02 PM To: [EMAIL PROTECTED] Subject: RE: ArrayList vs. Vector they aren't synchronised, tomcats thread pooling may cause unpredictable numbers of threads to have access to your objects, even once the servlets method has returned(unless you use single thread model). What do you stand to gain at the expense of the risk? -Original Message- From: Hunter Hillegas [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 8:37 PM To: Tomcat User List Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
Re: ArrayList vs. Vector
I recently re-wrote some older java code (jdk 1.1 based) and one of my goals was to use the new collection classes. I moved all of my Hashtables/Vectors that didn't need to be thread safe to HashMaps/ArrayLists. No problems under Tomcat. I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? It just seems easier to me to use Vectors/Hashtables, since they're already internally synchronized. Thanks, --jeff - Original Message - From: Hunter Hillegas [EMAIL PROTECTED] To: Tomcat User List [EMAIL PROTECTED] Sent: Monday, April 23, 2001 12:36 PM Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
Re: ArrayList vs. Vector
But that would only apply to objects kept in the application or session scope, yes? If an object a new object is created and placed in the request scope, it's only going to be accessed by one user (that request) right? Hunter From: Tim O'Neil [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] Date: Mon, 23 Apr 2001 13:47:01 -0700 To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector At 01:09 PM 4/23/2001 -0700, you wrote: This leads to a new question... What impact does synchronization have on Web applications? Where is it necessary? Well, the only good use of a collection is for keeping tabs on a group of data records, right? Well, what happens if one web user hits your baseball averages server and updates Cardinal Albert Pujols batting average while another user tries to update at exactly the same time? This is what synchronization is for.
Re: ArrayList vs. Vector
The biggest place I worry about synchronization in servlets is in static (class) variables. In my previous post, I said I migrated a lot of my code to the new Collection classes. To be more specific, I migrated my instance variables and left my class variables as Vectors/Hashtables. Thanks, --jeff - Original Message - From: Tim O'Neil [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 1:47 PM Subject: Re: ArrayList vs. Vector At 01:09 PM 4/23/2001 -0700, you wrote: This leads to a new question... What impact does synchronization have on Web applications? Where is it necessary? Well, the only good use of a collection is for keeping tabs on a group of data records, right? Well, what happens if one web user hits your baseball averages server and updates Cardinal Albert Pujols batting average while another user tries to update at exactly the same time? This is what synchronization is for.
Re: ArrayList vs. Vector
At 02:06 PM 4/23/2001 -0700, you wrote: I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? It just seems easier to me to use Vectors/Hashtables, since they're already internally synchronized. I do this when I'm concerned about concurrency and am using a non-thread-safe class, and although I can't say its any more than an aside comment (in other words I've hardly done exhaustive testing) I haven't had any problems.
Re: ArrayList vs. Vector
At 02:07 PM 4/23/2001 -0700, you wrote: But that would only apply to objects kept in the application or session scope, yes? If an object a new object is created and placed in the request scope, it's only going to be accessed by one user (that request) right? Keep thinking that if you write single thread model servlets.
RE: ArrayList vs. Vector
I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? I'd expect Vector/Hashtable to be a little faster: they subclass ArrayList/HashMap, where synchronized Collections are proxies. But, really, neither one works. Any places you'd use a collection, it's usually insufficient to synchronize the specific access: what you need to do is synchronize a block of accesses. For instance, here's a simple cache of Foo objects (each identified by name), implemented with a Hashtable: Hashtable fooCache = new Hashtable(); public Foo getFoo(String name) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } If you only want one Foo allocated per name--always--this caching mechanism will break down. You'd need to synchronize around _all_ accesses to the fooCache, whether it's a Hashtable or a HashMap. In other words, synchronizing the table's access methods don't buy you anything--you need to synchronize _across_ calls. And once you synchronize it correctly, synchronizing specific methods just slows you down. So, you should implement the above code as, HashMap fooCache = new HashMap(); public Foo getFoo(String name) { synchronized (fooCache) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } } This may be a peculiar case for you, but you're probably doing something which needs similar synchronization--whatever kind of collection you're using. -- Bill K. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 2:06 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector I recently re-wrote some older java code (jdk 1.1 based) and one of my goals was to use the new collection classes. I moved all of my Hashtables/Vectors that didn't need to be thread safe to HashMaps/ArrayLists. No problems under Tomcat. I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? It just seems easier to me to use Vectors/Hashtables, since they're already internally synchronized. Thanks, --jeff - Original Message - From: Hunter Hillegas [EMAIL PROTECTED] To: Tomcat User List [EMAIL PROTECTED] Sent: Monday, April 23, 2001 12:36 PM Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
Re: ArrayList vs. Vector
So if not, that's not the case? Thanks. Hunter From: Tim O'Neil [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] Date: Mon, 23 Apr 2001 14:26:08 -0700 To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector At 02:07 PM 4/23/2001 -0700, you wrote: But that would only apply to objects kept in the application or session scope, yes? If an object a new object is created and placed in the request scope, it's only going to be accessed by one user (that request) right? Keep thinking that if you write single thread model servlets.
Re: ArrayList vs. Vector
Ok, that makes sense, but I don't think it applies to my case. Most of the caching I'm doing is read-only for the servlet. I pull the info out of my database during the init and spawn a thread to refresh the Hashtable every six hours. By refresh, I mean I'm creating a new Hashtable object and replacing the current cache with the new object. I'm assuming that replacing the entire object won't wreak havoc on any current accesses -- that the replace will wait until any current synchronized accesses have finished. That may be a bad assumption. In the one instance in my code where I'm doing something similar to what you have, I'm still pulling info from my database to create the object to be cached. If two requests come in nearly simultaneously, they'll both pull the same info from my database and the second will overwrite the first in my cache. No big deal, because they both contain the same info/same key. I'm not sure if synchronizing the whole block is worthwhile in this case. Any strong argument why I should? Thanks, --jeff - Original Message - From: William Kaufman [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 2:39 PM Subject: RE: ArrayList vs. Vector I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? I'd expect Vector/Hashtable to be a little faster: they subclass ArrayList/HashMap, where synchronized Collections are proxies. But, really, neither one works. Any places you'd use a collection, it's usually insufficient to synchronize the specific access: what you need to do is synchronize a block of accesses. For instance, here's a simple cache of Foo objects (each identified by name), implemented with a Hashtable: Hashtable fooCache = new Hashtable(); public Foo getFoo(String name) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } If you only want one Foo allocated per name--always--this caching mechanism will break down. You'd need to synchronize around _all_ accesses to the fooCache, whether it's a Hashtable or a HashMap. In other words, synchronizing the table's access methods don't buy you anything--you need to synchronize _across_ calls. And once you synchronize it correctly, synchronizing specific methods just slows you down. So, you should implement the above code as, HashMap fooCache = new HashMap(); public Foo getFoo(String name) { synchronized (fooCache) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } } This may be a peculiar case for you, but you're probably doing something which needs similar synchronization--whatever kind of collection you're using. -- Bill K. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 2:06 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector I recently re-wrote some older java code (jdk 1.1 based) and one of my goals was to use the new collection classes. I moved all of my Hashtables/Vectors that didn't need to be thread safe to HashMaps/ArrayLists. No problems under Tomcat. I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? It just seems easier to me to use Vectors/Hashtables, since they're already internally synchronized. Thanks, --jeff - Original Message - From: Hunter Hillegas [EMAIL PROTECTED] To: Tomcat User List [EMAIL PROTECTED] Sent: Monday, April 23, 2001 12:36 PM Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
RE: ArrayList vs. Vector
You're right: 2) Using a (self-synchronized) Hashtable will be safe for the two cases you cite. The only problem is that you load some object(s) more often than you have to. (And that might be bad enough, if it's a large object,...) -- Bill K. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 3:54 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector Ok, that makes sense, but I don't think it applies to my case. Most of the caching I'm doing is read-only for the servlet. I pull the info out of my database during the init and spawn a thread to refresh the Hashtable every six hours. By refresh, I mean I'm creating a new Hashtable object and replacing the current cache with the new object. I'm assuming that replacing the entire object won't wreak havoc on any current accesses -- that the replace will wait until any current synchronized accesses have finished. That may be a bad assumption. In the one instance in my code where I'm doing something similar to what you have, I'm still pulling info from my database to create the object to be cached. If two requests come in nearly simultaneously, they'll both pull the same info from my database and the second will overwrite the first in my cache. No big deal, because they both contain the same info/same key. I'm not sure if synchronizing the whole block is worthwhile in this case. Any strong argument why I should? Thanks, --jeff - Original Message - From: William Kaufman [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 2:39 PM Subject: RE: ArrayList vs. Vector I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? I'd expect Vector/Hashtable to be a little faster: they subclass ArrayList/HashMap, where synchronized Collections are proxies. But, really, neither one works. Any places you'd use a collection, it's usually insufficient to synchronize the specific access: what you need to do is synchronize a block of accesses. For instance, here's a simple cache of Foo objects (each identified by name), implemented with a Hashtable: Hashtable fooCache = new Hashtable(); public Foo getFoo(String name) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } If you only want one Foo allocated per name--always--this caching mechanism will break down. You'd need to synchronize around _all_ accesses to the fooCache, whether it's a Hashtable or a HashMap. In other words, synchronizing the table's access methods don't buy you anything--you need to synchronize _across_ calls. And once you synchronize it correctly, synchronizing specific methods just slows you down. So, you should implement the above code as, HashMap fooCache = new HashMap(); public Foo getFoo(String name) { synchronized (fooCache) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } } This may be a peculiar case for you, but you're probably doing something which needs similar synchronization--whatever kind of collection you're using. -- Bill K. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 2:06 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector I recently re-wrote some older java code (jdk 1.1 based) and one of my goals was to use the new collection classes. I moved all of my Hashtables/Vectors that didn't need to be thread safe to HashMaps/ArrayLists. No problems under Tomcat. I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? It just seems easier to me to use Vectors/Hashtables, since they're already internally synchronized. Thanks, --jeff - Original Message - From: Hunter Hillegas [EMAIL PROTECTED] To: Tomcat User List [EMAIL PROTECTED] Sent: Monday, April 23, 2001 12:36 PM Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
RE: ArrayList vs. Vector
Hello, I'm not really sure why you are doing what you are doing but if you need key value pairs that can be called arbitrarily you may consider a HashMap rather then a HashTable. Perhaps you are creating a data or object pool..? If everything is truly read only by the servlets it doesn't seem to me that you would need the synchronization overhead except for that brief moment when you update the data structure. In any case a HashMap is fail-fast so if the data changes after an iterator object is created and before it finishes it's task the iterator will safely fail -- you could easily write into your code to throw and catch an error there and have the process loop by using a flag during updates. I would imagine were talking about milliseconds. Give your unique update thread a high priority -- (Thread.NORM_PRIORITY + 2) Are the individual elements of various object types? If not perhaps a simple array would be the quickest as you would eliminate the casting overhead. If you order your array you can use a binary search or simply create a position index to call elements directly from their position in the array. It sounds like the size of your data object is constant. If you are iterating through your data an array would certainly be faster. Usually simpler is better. I wonder how much overhead creating an iterator costs? Perhaps create an iterator pool. Just a few thoughts. Sounds like you aren't having any troubles. Best Regards, Craig Oh yeahow's your tomcat? Mine are doing great. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 3:54 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector Ok, that makes sense, but I don't think it applies to my case. Most of the caching I'm doing is read-only for the servlet. I pull the info out of my database during the init and spawn a thread to refresh the Hashtable every six hours. By refresh, I mean I'm creating a new Hashtable object and replacing the current cache with the new object. I'm assuming that replacing the entire object won't wreak havoc on any current accesses -- that the replace will wait until any current synchronized accesses have finished. That may be a bad assumption. In the one instance in my code where I'm doing something similar to what you have, I'm still pulling info from my database to create the object to be cached. If two requests come in nearly simultaneously, they'll both pull the same info from my database and the second will overwrite the first in my cache. No big deal, because they both contain the same info/same key. I'm not sure if synchronizing the whole block is worthwhile in this case. Any strong argument why I should? Thanks, --jeff - Original Message - From: William Kaufman [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 2:39 PM Subject: RE: ArrayList vs. Vector I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? I'd expect Vector/Hashtable to be a little faster: they subclass ArrayList/HashMap, where synchronized Collections are proxies. But, really, neither one works. Any places you'd use a collection, it's usually insufficient to synchronize the specific access: what you need to do is synchronize a block of accesses. For instance, here's a simple cache of Foo objects (each identified by name), implemented with a Hashtable: Hashtable fooCache = new Hashtable(); public Foo getFoo(String name) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } If you only want one Foo allocated per name--always--this caching mechanism will break down. You'd need to synchronize around _all_ accesses to the fooCache, whether it's a Hashtable or a HashMap. In other words, synchronizing the table's access methods don't buy you anything--you need to synchronize _across_ calls. And once you synchronize it correctly, synchronizing specific methods just slows you down. So, you should implement the above code as, HashMap fooCache = new HashMap(); public Foo getFoo(String name) { synchronized (fooCache) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } } This may be a peculiar case for you, but you're probably doing something which needs similar synchronization--whatever kind of collection you're using. -- Bill K. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 2:06 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector I
RE: ArrayList vs. Vector
I ran some test code (see attachment) and noticed that if you are adding small numbers of objects to the list (Vector or ArrayList) then the Vector is faster than the synched ArrayList. As the number of objects to insert increases, so does the ArrayLists performance. I ran two tests, one with iterations set to 10K (create 1 objects and add them to the list) and one with it set to 100K. In the first test, the Vector was faster but the List won out in the long run. Hope this helps. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 5:06 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector I recently re-wrote some older java code (jdk 1.1 based) and one of my goals was to use the new collection classes. I moved all of my Hashtables/Vectors that didn't need to be thread safe to HashMaps/ArrayLists. No problems under Tomcat. I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? It just seems easier to me to use Vectors/Hashtables, since they're already internally synchronized. Thanks, --jeff - Original Message - From: Hunter Hillegas [EMAIL PROTECTED] To: Tomcat User List [EMAIL PROTECTED] Sent: Monday, April 23, 2001 12:36 PM Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter VectorArrayListTest.java
Re: ArrayList vs. Vector
Hi Iain, Thanks. I was under the same impression. I'm not adding (or iterating over) even 10,000 objects, so I'll stick with the Vector. --jeff - Original Message - From: Iain Lowe [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 4:45 PM Subject: RE: ArrayList vs. Vector I ran some test code (see attachment) and noticed that if you are adding small numbers of objects to the list (Vector or ArrayList) then the Vector is faster than the synched ArrayList. As the number of objects to insert increases, so does the ArrayLists performance. I ran two tests, one with iterations set to 10K (create 1 objects and add them to the list) and one with it set to 100K. In the first test, the Vector was faster but the List won out in the long run. Hope this helps. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 5:06 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector I recently re-wrote some older java code (jdk 1.1 based) and one of my goals was to use the new collection classes. I moved all of my Hashtables/Vectors that didn't need to be thread safe to HashMaps/ArrayLists. No problems under Tomcat. I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? It just seems easier to me to use Vectors/Hashtables, since they're already internally synchronized. Thanks, --jeff - Original Message - From: Hunter Hillegas [EMAIL PROTECTED] To: Tomcat User List [EMAIL PROTECTED] Sent: Monday, April 23, 2001 12:36 PM Subject: ArrayList vs. Vector I use Vectors in some parts of my Web app and I'm thinking about using ArrayLists instead... Any caveats to using them in a Web app environment? Hunter
Re: ArrayList vs. Vector
Hi Craig, Thanks. I may re-investigate the necessity of using a synchronized object. Mostly, I'm using Hashtables just in case -- because they're class variables. I've moved all my instance variables to ArrayLists and HashMaps. As for arrays, if I were trying to squeak out a little more performance I'd definitely consider them. However, I was able to achieve 54 inserts/sec running the code the way it is on a crappy little Linux box (128MB RAM, IDE drive, 430MHz Celeron, DB + Webserver + Tomcat all on the same machine). So, I'm pretty happy with the speed -- at least, happy enough not to take on arrays as opposed to Lists and Maps. :)Besides, there is one cache I'm using that could grow during runtime before doing a full refresh from the database. Thanks, --jeff - Original Message - From: Craig O'Brien [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 4:39 PM Subject: RE: ArrayList vs. Vector Hello, I'm not really sure why you are doing what you are doing but if you need key value pairs that can be called arbitrarily you may consider a HashMap rather then a HashTable. Perhaps you are creating a data or object pool..? If everything is truly read only by the servlets it doesn't seem to me that you would need the synchronization overhead except for that brief moment when you update the data structure. In any case a HashMap is fail-fast so if the data changes after an iterator object is created and before it finishes it's task the iterator will safely fail -- you could easily write into your code to throw and catch an error there and have the process loop by using a flag during updates. I would imagine were talking about milliseconds. Give your unique update thread a high priority -- (Thread.NORM_PRIORITY + 2) Are the individual elements of various object types? If not perhaps a simple array would be the quickest as you would eliminate the casting overhead. If you order your array you can use a binary search or simply create a position index to call elements directly from their position in the array. It sounds like the size of your data object is constant. If you are iterating through your data an array would certainly be faster. Usually simpler is better. I wonder how much overhead creating an iterator costs? Perhaps create an iterator pool. Just a few thoughts. Sounds like you aren't having any troubles. Best Regards, Craig Oh yeahow's your tomcat? Mine are doing great. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 3:54 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector Ok, that makes sense, but I don't think it applies to my case. Most of the caching I'm doing is read-only for the servlet. I pull the info out of my database during the init and spawn a thread to refresh the Hashtable every six hours. By refresh, I mean I'm creating a new Hashtable object and replacing the current cache with the new object. I'm assuming that replacing the entire object won't wreak havoc on any current accesses -- that the replace will wait until any current synchronized accesses have finished. That may be a bad assumption. In the one instance in my code where I'm doing something similar to what you have, I'm still pulling info from my database to create the object to be cached. If two requests come in nearly simultaneously, they'll both pull the same info from my database and the second will overwrite the first in my cache. No big deal, because they both contain the same info/same key. I'm not sure if synchronizing the whole block is worthwhile in this case. Any strong argument why I should? Thanks, --jeff - Original Message - From: William Kaufman [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 2:39 PM Subject: RE: ArrayList vs. Vector I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? I'd expect Vector/Hashtable to be a little faster: they subclass ArrayList/HashMap, where synchronized Collections are proxies. But, really, neither one works. Any places you'd use a collection, it's usually insufficient to synchronize the specific access: what you need to do is synchronize a block of accesses. For instance, here's a simple cache of Foo objects (each identified by name), implemented with a Hashtable: Hashtable fooCache = new Hashtable(); public Foo getFoo(String name) { Foo foo = (Foo)fooCache.get(name); if (foo == null) { foo = new Foo(name); fooCache.put(name, foo); } return foo; } If you only want one Foo allocated per name--always--this caching mechanism will break down. You'd need to synchronize around _all_ accesses to the fooCache, whether it's
RE: ArrayList vs. Vector
Hey Jeff, Thanks for the shout out. I kind of caught the beginning and end of the conversation and realized after I posted that I was a little bit out of the conversation I kind of obsess about performance at times but it is always interesting to see what people think. (that's what happens when you're head down too much) Honestly Vectors and HashTables are probably fine and plenty fast for most applications, especially web apps with limited size. I would think that you are already %600 faster then CGI so what the hey... we're only talking about a hundred milliseconds anyway. Good luck with your endeavors, Fraternally, Craig -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 9:35 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector Hi Craig, Thanks. I may re-investigate the necessity of using a synchronized object. Mostly, I'm using Hashtables just in case -- because they're class variables. I've moved all my instance variables to ArrayLists and HashMaps. As for arrays, if I were trying to squeak out a little more performance I'd definitely consider them. However, I was able to achieve 54 inserts/sec running the code the way it is on a crappy little Linux box (128MB RAM, IDE drive, 430MHz Celeron, DB + Webserver + Tomcat all on the same machine). So, I'm pretty happy with the speed -- at least, happy enough not to take on arrays as opposed to Lists and Maps. :)Besides, there is one cache I'm using that could grow during runtime before doing a full refresh from the database. Thanks, --jeff - Original Message - From: Craig O'Brien [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 4:39 PM Subject: RE: ArrayList vs. Vector Hello, I'm not really sure why you are doing what you are doing but if you need key value pairs that can be called arbitrarily you may consider a HashMap rather then a HashTable. Perhaps you are creating a data or object pool..? If everything is truly read only by the servlets it doesn't seem to me that you would need the synchronization overhead except for that brief moment when you update the data structure. In any case a HashMap is fail-fast so if the data changes after an iterator object is created and before it finishes it's task the iterator will safely fail -- you could easily write into your code to throw and catch an error there and have the process loop by using a flag during updates. I would imagine were talking about milliseconds. Give your unique update thread a high priority -- (Thread.NORM_PRIORITY + 2) Are the individual elements of various object types? If not perhaps a simple array would be the quickest as you would eliminate the casting overhead. If you order your array you can use a binary search or simply create a position index to call elements directly from their position in the array. It sounds like the size of your data object is constant. If you are iterating through your data an array would certainly be faster. Usually simpler is better. I wonder how much overhead creating an iterator costs? Perhaps create an iterator pool. Just a few thoughts. Sounds like you aren't having any troubles. Best Regards, Craig Oh yeahow's your tomcat? Mine are doing great. -Original Message- From: Jeff Kilbride [mailto:[EMAIL PROTECTED]] Sent: Monday, April 23, 2001 3:54 PM To: [EMAIL PROTECTED] Subject: Re: ArrayList vs. Vector Ok, that makes sense, but I don't think it applies to my case. Most of the caching I'm doing is read-only for the servlet. I pull the info out of my database during the init and spawn a thread to refresh the Hashtable every six hours. By refresh, I mean I'm creating a new Hashtable object and replacing the current cache with the new object. I'm assuming that replacing the entire object won't wreak havoc on any current accesses -- that the replace will wait until any current synchronized accesses have finished. That may be a bad assumption. In the one instance in my code where I'm doing something similar to what you have, I'm still pulling info from my database to create the object to be cached. If two requests come in nearly simultaneously, they'll both pull the same info from my database and the second will overwrite the first in my cache. No big deal, because they both contain the same info/same key. I'm not sure if synchronizing the whole block is worthwhile in this case. Any strong argument why I should? Thanks, --jeff - Original Message - From: William Kaufman [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, April 23, 2001 2:39 PM Subject: RE: ArrayList vs. Vector I still use Vectors/Hashtables when I need thread safety, though. Does anyone know if it's faster/better to wrap one of the new collection classes in a Collections.synchronized* class instead? I'd expect Vector/Hashtable to be a little faster: they subclass ArrayList