You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Hunter Hillegas <li...@lastonepicked.com> on 2001/04/23 21:36:52 UTC

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

Posted by Dennis Doubleday <dd...@mail.yourfit.com>.
At 05:54 PM 4/23/01, you wrote:
>So if not, that's not the case?
>
>Thanks.
>Hunter
>
> > From: "Tim O'Neil" <ti...@xythos.com>
> > Reply-To: tomcat-user@jakarta.apache.org
> > Date: Mon, 23 Apr 2001 14:26:08 -0700
> > To: tomcat-user@jakarta.apache.org
> > 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: dday@yourfit.com
yourfit.com, Inc.           web: http://www.yourfit.com/


Re: ArrayList vs. Vector

Posted by Hunter Hillegas <li...@lastonepicked.com>.
So if not, that's not the case?

Thanks.
Hunter

> From: "Tim O'Neil" <ti...@xythos.com>
> Reply-To: tomcat-user@jakarta.apache.org
> Date: Mon, 23 Apr 2001 14:26:08 -0700
> To: tomcat-user@jakarta.apache.org
> 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

Posted by Tim O'Neil <ti...@xythos.com>.
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

Posted by Jeff Kilbride <je...@kilbride.com>.
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" <ti...@xythos.com>
To: <to...@jakarta.apache.org>
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

Posted by Hunter Hillegas <li...@lastonepicked.com>.
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" <ti...@xythos.com>
> Reply-To: tomcat-user@jakarta.apache.org
> Date: Mon, 23 Apr 2001 13:47:01 -0700
> To: tomcat-user@jakarta.apache.org
> 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

Posted by Tim O'Neil <ti...@xythos.com>.
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

Posted by Hunter Hillegas <li...@lastonepicked.com>.
This leads to a new question...

What impact does synchronization have on Web applications? Where is it
necessary?

> From: "Tim O'Neil" <ti...@xythos.com>
> Reply-To: tomcat-user@jakarta.apache.org
> Date: Mon, 23 Apr 2001 13:03:25 -0700
> To: tomcat-user@jakarta.apache.org
> 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

Posted by Tim O'Neil <ti...@xythos.com>.
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

Posted by Nick Christopher <nw...@visionics.com>.
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

Posted by Tim O'Neil <ti...@xythos.com>.
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

Posted by Jeff Kilbride <je...@kilbride.com>.
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" <il...@videotron.ca>
To: <to...@jakarta.apache.org>
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 10000 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:jeff@kilbride.com]
> Sent: Monday, April 23, 2001 5:06 PM
> To: tomcat-user@jakarta.apache.org
> 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" <li...@lastonepicked.com>
> To: "Tomcat User List" <to...@jakarta.apache.org>
> 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

Posted by Iain Lowe <il...@videotron.ca>.
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 10000 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:jeff@kilbride.com]
Sent: Monday, April 23, 2001 5:06 PM
To: tomcat-user@jakarta.apache.org
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" <li...@lastonepicked.com>
To: "Tomcat User List" <to...@jakarta.apache.org>
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

Posted by Jeff Kilbride <je...@kilbride.com>.
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" <li...@lastonepicked.com>
To: "Tomcat User List" <to...@jakarta.apache.org>
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

Posted by Craig O'Brien <cr...@dynamic-apps.com>.
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:lists@lastonepicked.com]
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

Posted by Danny Angus <da...@thought.co.uk>.
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:lists@lastonepicked.com]
> 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

Posted by "Jerzy Wirecki (Jerry)" <je...@internetphotonics.com>.
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:lists@lastonepicked.com]
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