The Artima Developer Community
Sponsored Link

Java Answers Forum
Arrays vs. Vectors

3 replies on 1 page. Most recent reply: Apr 21, 2002 4:53 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Arrays vs. Vectors Posted: Apr 19, 2002 11:03 AM
Reply to this message Reply
Advertisement
I need to be able to keep track of different TCP connections, and I'm not sure whether to use an array or vector or something else. Let me clarify: I have (or will have) a class that creates a TCP connection, and that class will run as a seperate thread. That class will send info on that connection, as well as monitor for incoming info from that connection. I will then have another class that keeps track of all those connection classes. (The reason I'm doing this is because some of the connections will at times redirect me to create a new connection with another server, but yet I'll still need to hang onto the old connection as well).

My main question is from a overhead, and speed point of view, is it better to use an array or vector (or some other container class)? My not so important question is whether what I want to do is a good idea - that is, have a seperate object running as its own thread for every single connection I make? If not, how else should I managae each connection?

Thanks for the help.
Hiran


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Arrays vs. Vectors Posted: Apr 20, 2002 1:52 PM
Reply to this message Reply
So what you need is a Connection Pool ?
Why don't you look at the one used by JBoss. It's an OpenSource one I believe, so ...

Thomas,

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: Arrays vs. Vectors Posted: Apr 21, 2002 11:57 AM
Reply to this message Reply
Adding a few more thoughts,

...I need to be able to keep track of different TCP connections, and I'm not sure whether to use an array or vector or something else. Let me clarify: I have (or will have) a class that creates a TCP connection, and that class will run as a seperate thread. That class will send info on that connection, as well as monitor for incoming info from that connection. I will then have another class that keeps track of all those connection classes. (The reason I'm doing this is because some of the connections will at times redirect me to create a new connection with another server, but yet I'll still need to hang onto the old connection as well)...


The whole of your first para can be summarized as Connection Pool(which Thomas has already pointed). Your program requests the pool for a connection object but does not know anything more. The pool is responsible for creating/saving the connection object so that it can be reused (if at all possible) at a later point of time, instead of creating a new one, which could be expensive.

However, keeping track of those objects could be a tedious task. If you use an array or ArrayList, and a new connection is requested, you need to do atleast these things:
1. Loop through the array to search for an unused connection.
2. If not, you need to create a new object and store in the array so check for the bounds of that array.
3. If array limit is reached, create a bigger array, copy old values into newer one.
4. Since you are in a multithreaded environment, (atleast the class that creates connections) the array needs to be synchronized and so do the accessor methods.
A Vector, which is an array of objects, does most of it by default. Ultimately it boils down to:
Using array/ArrayList is faster than a Vector. But they are not synchronized.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Arrays vs. Vectors Posted: Apr 21, 2002 4:53 PM
Reply to this message Reply
May I in return refere & amend your post Jay !
:-p

I personnaly believe you don't have to be bright to make/code a Connection Pool, so I will just put next to your post some ideas
(you just need to be stupid to make something that already exists :-D, but this is just teasing for free).

>
> However, keeping track of those objects could be a
> tedious task. If you use an array or
> ArrayList, and a new connection is requested,
> you need to do atleast these things:
> 1. Loop through the array to search for an unused
> connection.
>

This can be pretty quick, as you could implement you own "Socket" class so the equals() & hash () methods are implemented. The former uses the latter to quickly evaluate that the two connections are not equals. One has to keep in mind that equaling of two instances can usually pretty nasty, but you can make the people not feeling it as you can discart most the equalling. Also by making your own SocketWrapper class you can usually easily simplyfy the equaling process to :
public boolean equals (Object o)
{
  if (hash() != o.hash()) // My throw a NullPointerException
    return false;
 
  MyWrapper trgt = (MyWrapper)o; // May a ClassCastException
// According to your environment you may alter the way the evaluation is made 
// The IP is considered to have the form A.B.C.D.
// It's probably faster to evaluate port / D / C / B / A it's just a question 
// of versatility in the values.
// Just don't use the "&&" here ! With only one "&", as soon as one is evaluate to 
// false the evaluation of the expression is considered (rightfully)
// as false 
  if ( this.IP_A ==  trgt.getIPA () 
       & this.IP_B ==  trgt.getIPB ()
       & this.IP_C ==  trgt.getIPC ()
       & this.IP_D ==  trgt.getIPD () 
       & this.port ==  trgt.getPort () 
       & ... ) // beware that both end of the Socket must be equal 
    return false;
 
  return true;
}


>
> 2. If not, you need to create a new object and store
> in the array so check for the bounds of that array.
>

Sorry, I do not agree with you here Jay.
The value is normally predefined inthe Connection pool settings (some sort of MAX_NBR_OF_CONNECCTION).
It that's the case you avoifd that also a static method on the ConnectionPool may quickly to a check & return a custom made exception.

>
> 3. If array limit is reached, create a bigger array,
> copy old values into newer one.
>

This is trivial

>
> 4. Since you are in a multithreaded environment,
> (atleast the class that creates connections) the
> array needs to be synchronized and so do the accessor
> methods.


This is however pretty hard business
to validate that the code is multi-threaded.

I can only advise to use JUnit (http://www.junit.org) & more specifically JUnitPerf (a JUnit extension available at www.clarkware.com). Mike is a very helpfull fellow (but unfortunately very busy).


> A Vector, which is an array of objects, does
> most of it by default. Ultimately it boils down to:
> Using array/ArrayList is faster than a
> Vector. But they are not synchronized.
>

This is an excellent remark !!!
Using already synchronized repository (Vector, ...) may help to simplify the coding but it will be slower !

One thing you should not forget when coding the Pool ... People will have to close () the connection to notify the Pool that the connection may be released. Give a look at the following class that may help you with that javax.swing.Timer

Good luck & keep us inform !

Flat View: This topic has 3 replies on 1 page
Topic: finding classpath visible to classloader Previous Topic   Next Topic Topic: JTable - Save preferences

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use