Jay Kandy
Posts: 77
Nickname: jay
Registered: Mar, 2002
|
|
Re: Arrays vs. Vectors
|
Posted: Apr 21, 2002 11:57 AM
|
|
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.
|
|