Hi Folks, I know that ArrayList is essentially an unsynchronized Vector. And Since Vector is synchronized, it is slower than ArrayList. When synchronization is needed, Vector is chosen over ArrayList. Now, could you please tell me, ok these are the situations where Vector must be used instead of ArrayList. Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal
Vector should never be used in new code, except if you're using an API or library method that requires you to pass a Vector as a method argument. If you need a synchronized List implementation, you can get one from the method Collections.synchronizedList().
Originally posted by Ernest Friedman-Hill: Vector should never be used in new code, except if you're using an API or library method that requires you to pass a Vector as a method argument. If you need a synchronized List implementation, you can get one from the method Collections.synchronizedList().
Could you please explain me what do you mean by "new code". Could you tell me your real world experience where you had no choice other than use a synchronized List. I am trying to see what would be the situation like where we must use synchronized list. thanks again.
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
Re the "not in new code" idea ... Vector and HashTable were very early Java classes that Sun quickly decided to replace with better ones. They just weren't very well designed from the API and naming point of view. Unfortunately everybody learns Vector and HashTable in introductory books and tutorials and keeps on using them. Learn to love the Collection classes like ArrayList and HashMap. You will just be that much cooler than the people who keep writing Vectors.
Here's a Collections Crib Sheet I copied from a Sun newsletter cause I can't keep all the collections classes in my head. [ July 02, 2004: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Actually "old code" can be also recognized from the name clue Hashtable, the so-called 'new' code would have used 'HashTable', like in HashMap.
old new -------- --------- Hashtable ==> HashMap Vector ==> ArrayList
the new Java 1.2 code uses 'em.. ciao, R�stem.
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
posted
0
To answer your underlying question, you normally synchronize a method when it can be invoked by two or more threads at the same time. This is because your data can be corrupted if two threads are updating the same varables and objects at the same time.
The example usually given is deducting a cash withdrawal from your account balance (balance = balance - debit ; ) . Let's say you started with $100 and both you and you wife, at different ATM's, take out $20. The first thread retrieves balance, $100. The second thread retrieves balance, $100. The first thread subtracts $20, leaving $80. The first thread stores $80 in balance. The second thread subtracts $20 from its copy of balance, leaving $80. The second thread stores $80 in balance.
You and your wife are happy, but the bank is out $20.
The same thing can happen to the indexes in an ArrayList if two threads add an element at the same time. It gets even worse if the ArrayList is full and both threads make separate new copies of the private array Object[] inside your ArrayList.
Actually, Vector is now just a version of ArrayList with synchronized added, but it is unfashionable to use obsolescent classes when new ones are available.
By the way, real experts synchronize just the part of their code that is vulnerable, but that is tricky. [ January 13, 2005: Message edited by: Mike Gershman ]
Mike Gershman
SCJP 1.4, SCWCD in process
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
Originally posted by Tulsi Rai: Hi Ernest, Thanks for your help.
Could you please explain me what do you mean by "new code".
I think "new code" means code that you are writing right now. Where as "old code" means code that you (or someone else) wrote yesterday, last week, last year, or ten years ago.