Carefully modify the size of the collection

xiaoxiao2021-03-06  121

The most commonly used in Java is a collection, and it is in performance, now most people no longer use the vector synchronization of this method. The new set library brings faster performance, and it also brings more possible misuse purposes. The improvement of the performance of the new set library is mainly to remove the method synchronization. It is conceivable that there is inevitable data consistency when synchronous access. In order to make the program do not cause other errors because of the inconsistencies of data, the new set library uses a fast failure mechanism when designing, is that each traversal collection will determine whether the current collective size is modified, if modified , Immediately throw the ConcurrentModificationException and stop procedures continue to run. Therefore, after the set initialization is completed, it should be avoided to be modified on the other. Look into this example: package com.bhr.ioat.testCollection; import java.util. *; Public class testremove {public static void main (string [] args) {Collection CLTN = New ArrayList (); for (int i = 0 I <100000; i ) {CLTN.Add (New Integer (i)); NEW OTHERTHREAD (CLTN) .Start (); try {thread.sleep (1000); // Sleep 1 Second, in Order To Ensure THE New Thread Start Up.} catch (exception e) {E.PrintStackTrace ();} iter = CLTN.ITerator (); while (it.hasnext ()) {Object obj = it.next (); CLTN.Remove obj); //it.remove (); System.out.println ( "Remove one element from collection"); break;}}} class OtherThread extends Thread {public Collection cltn_; public OtherThread (Collection cltn) {cltn_ = cltn ; // CLTN_ = ("(arraylist) CLTN) .clone ());} public void run () {iplic void Run () {iplic void Run () {iplic void Run () {iplic void Run () {iplic it = CLTN_.ITERATOR (); while (it.hasnext ()) {Object Obj = IT .next (); system.out.println (obj);}}} The program is simple, starting to initialize a 100,000 size ArrayList, then pass it to another class, then delete the collection One element, you will find that it will throw the ConcurrentModificationException exception. Isn't it possible to delete an element? Do you continue to use Vector? Of course, it is not the meaning of no new collection.

There are two, (1) collections are only used at one place, which naturally has no concurrency problems, but it is not bold to modify, if you remove the elements in the collection, you must call the Remove method of the item, not the Collection The Remove method, the former will modify a value of the Iterator, so that the loop is not modified, and can continue, and the latter does not modify the value in the Iterator, continue the loop. Exception. As for the addition element, the corresponding method is not provided in the Iterator, so if it is added to the loop, it can only be jumped out of the loop after it is added. (2) The same collection is used in multiple sites. Do not delete it. This situation should not modify the collection size. If you decide the size of the set size does not affect the normal logic of the program, then only clone is in use . The above is only a little experience, if you have a better way, you can communicate. E-mail: bbs_9527@yahoo.com.cn

转载请注明原文地址:https://www.9cbs.com/read-126116.html

New Post(0)