Carefully modify the size of the collection

zhaozj2021-02-16  52

Carefully modify the size of the collection

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 below:

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 ();

}

Iterator it = CLTN.Itemrator ();

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_ = (Collection) ((arraylist) CLTN) .clone ());

}

Public void run () {

Iterator it = CLTN_.ITerator ();

While (it.hasnext ()) {

Object obj = it.next ();

System.out.println (OBJ);

}

}

}

The program is very simple and started to initialize a 100,000 size ArrayList, then pass it to another class. Subsequently delete an element in the collection, you will find that it will throw the concurrentmodificationException.

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 just a summary of experience, if you have a better way, you can communicate, e-mail: zhvfeng@hotmail.com

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

New Post(0)