This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Another way is to do something like:(Note: I am guessing at what you put into your Vector - you may have to change the definition of records to suit your implementation.) This has the advantage that you are synchronizing on myVector for the shortest possible time, then doing the work on a local snapshot of myVector.
You could also use the clone() method to create a local copy of myVector - from memory the clone method does a deep copy (Vector overrides clone for just this purpose) - then you could iterate over the local copy in the same way that I used the array (but the array would probably be faster - it then becomes more of a code readability issue).
Origanally posted by John Smith Although Vector is a synchronized list, it's not thread safe for iteration through its elements.
The synchronization on Vector is usually not much help to the programmer, and we generally recommend against using it. Perhaps you could mention why you need a synchronized collection, and we can comment on whether Vector suits your purpose?