Java is the most commonly used collection, and is in performance considerations. Most people no longer use the Vector Method for synchronization. The new collection Library brings faster performance and more error usage possibilities. The performance improvement of the new collection library is mainly to remove the synchronization method. It can be imagined that there must be a data consistency problem during synchronous access. To prevent other errors caused by inconsistent data, the new collection library uses the Fast-Fail Mechanism during design ), this means that the current set size is determined to be modified every time the set is traversed. If the set size is modified, a ConcurrentModificationException is thrown immediately to stop the program from running. Therefore, after the set Initialization is complete, the size of the set should not be modified in another place. Let's look at the example below:
- PackageCom. bhr. ioat. testcollection;
- ImportJava. util .*;
- Public ClassTestRemove
- {
- Public Static VoidMain (String[] Args)
- {
- CollectionCltn =New ArrayList();
- For(IntI = 0; I <100000; I ++ ){
- Cltn. add (New Integer(I ));
- }
- NewOtherThread (cltn). start ();
- Try{
- Thread. Sleep (1000 );// Sleep 1 second, in order to ensure the new thread start up.
- }Catch(ExceptionE ){
- E. printStackTrace ();
- }
- IteratorIt = cltn. iterator ();
- While(It. hasNext ()){
- ObjectObj = it. next ();
- Cltn. remove (obj );
- // It. remove ();
- System. Out. println ("Remove one element from collection ");
- Break;
- }
- }
- }
- ClassOtherThreadExtends Thread
- {
- Public CollectionCltn _;
- PublicOtherThread (CollectionCltn ){
- Cltn _ = cltn;
- // Cltn _ = (Collection) (ArrayList) cltn). clone ());
- }
- Public VoidRun (){
- IteratorIt = cltn _. iterator ();
- While(It. hasNext ()){
- ObjectObj = it. next ();
- System. Out. println (obj );
- }
- }
- }
The program is very simple. Initialize an ArrayList of 100000 size and pass it to another class. Then, delete an element in the set. You will find that the ConcurrentModificationException will be thrown immediately.
Can't we delete elements? Should we continue using Vector? Of course not. It doesn't make sense if a new set appears. There are two solutions: (1) the set is only used in one place, which naturally has no concurrency problem, but it still cannot be modified. If the elements in the set are deleted in a loop, you must call the remove Method of Iterator instead of the remove method of Collection. The former will modify the value of Iterator after deletion, so that the loop can continue to process the set because it is not modified, the latter is called without modifying the value in the Iterator. If you continue the loop, an exception is thrown. As for adding elements, the Iterator does not provide the corresponding method. Therefore, if you add elements in the loop, you can only jump out of the loop after adding the elements. (2) If the same set is used in multiple places, simply do not delete it. In this case, the set size should not be modified. If you decide that the modification of the set size will not affect the normal logic of the program, in this case, you have to clone one.
The above just summed up a little practical experience, if you have a better way, You can exchange, E-mail: zhvfeng@hotmail.com