Hashtable Delete element, throw exception java.util.ConcurrentModificationException

Source: Internet
Author: User
Tags throw exception concurrentmodificationexception

A problem that I encountered today when I was searching for a Hashtable object, I started using the Hashtable iterator, and then executed it directly:

Hashtable.remove (key); Throw Exception Java.util.ConcurrentModificationException

Check out the Java API Doc, which is described below:

The Listiterator method of the Iterator returned by the iterator and the collection returned by all Hashtable's collection view methods is a quick failure: After the Iterator is created, if the hashtabl is structurally E is modified, Iterator will throw concurrentmodificationexception unless you modify it in any way at any time by Iterator its own removal or addition method. Therefore, in the face of concurrent changes, the Iterator will soon fail completely without risking any uncertain behavior at some uncertain time in the future. The enumeration returned by the Hashtable key and value method is not a quick failure.

hashtable<integer,integer> t = new hashtable<integer,integer> ();

for (int i=0;i<10;i++)
{
T.put (I,I*10);
}

System.out.println ("T.size ():" + t.size ());
System.out.println (T.get (6));

System.out.println ();

iterator<integer> i = T.keyset (). Iterator ();

while (I.hasnext ())
{
int key = I.next ();

if (key = = 3)
{
T.remove (key); So delete, throw exception java.util.ConcurrentModificationException
I.remove ();Only in this way can we successfully delete an element
}
}

System.out.println ("T.size ():" + t.size ());
System.out.println (T.get (6));

Use the above T.keyset (). iterator (); the way, if in multi-threaded case, will also throw java.util.ConcurrentModificationException

In the case of multithreading concurrency, add, modify, delete, and synchronized all over the time:

Public synchronized void Delkey (int key)
{
iterator<integer> i = T.keyset (). Iterator ();
while (I.hasnext ())
{
int abc = I.next ();

T.remove (key); Throw Exception Java.util.ConcurrentModificationException

if (abc = = key)
I.remove (); Only in this way can we successfully delete an element
}
}

When you call Delkey () in a multi-line, you must use: Synchronized before you define the Delkey () method to ensure that multithreaded calls are secure.

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------

There is also an earlier (and more primitive) security approach that uses enumeration, which is safe to perform both single-threaded and multi-threaded (note the following special instructions):

hashtable<integer,integer> t = new hashtable<integer,integer> ();

for (int i=0;i<10;i++)
{
T.put (I,I*10);
}

System.out.println ("T.size ():" + t.size ());
System.out.println (T.get (6));

System.out.println ();

enumeration<integer> e = T.keys ();
while (E.hasmoreelements ())
{
int key = E.nextelement ();

if (key = = 3)
T.remove (key);
}

System.out.println ("T.size ():" + t.size ());
System.out.println (T.get (6));

Special attention:

Although using enumeration to traverse elements is thread-safe, high concurrency increases, modifies, iterates, and does not throw any exceptions, but if you are traversing enumeration while deleting an element inside it will not throw any exception, but the result may not be what you think: List = New Vector<string> ();

List.add ("1");
List.add ("2");
List.add ("3");
List.add ("4");
List.add ("5");
List.add ("6");

enumeration<string> i = list.elements ();
while (I.hasmoreelements ())
{
String str = i.nextelement ();

System.out.println (str);

List.removeelement (str); Here to delete
}

Output Result:

1

3

5

I saw the result, and I was really surprised.

2011-04-18

Hashtable Delete element, throw exception java.util.ConcurrentModificationException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.