[Original] by using a collection iterator to be updated. Delete operation, there java.util.ConcurrentModificationException solution.

In the project there is a demand, need a Set type collection to meet the conditions to remove the object, then take it for granted thought to directly call Set the remove (Object o) method to delete the specified object can be.

However, when running the program, they found a mistake:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap $ HashIterator.nextEntry (Unknown Source)
at java.util.HashMap $ KeyIterator.next (Unknown Source)
at test.Test.main (Test.java: 23)

List can be seen from the API, the realization of such Collection is not synchronized, if you appear in multi-threaded applications simultaneously access and modify the operation occurs when the external operations require synchronization; Diaoyong Iterator Iterator object operations get multi-threading Set modification time is automatically invalidated and thrown java.util.ConcurrentModificationException. This implementation mechanism is fail-fast, on the external changes and can not provide any guarantee.

Iterator is working in a separate thread, Iterator is created when the establishment of a memory index table (single list), the index table points to the original object, when the number of objects to change the original time, the index table of content without same pace, so when the index pointer moves down, it will not find the object to be iterative, so an error occurs. List, Set, etc. are dynamic, variable number of objects in the data structure, but the Iterator is immutable one-way only in order to read, can not reverse operation of the data structure, when the Iterator pointing to the original data is changed, Iterator own the lost.
How can it meet demand, need to define a List, to save the need to remove the object.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of [Original] by using a collection iterator to be updated. Delete operation, there java.util.ConcurrentModificationException solution.

  • mac development environment to deploy ror

    I do not have any type of unix experience, so middle of the book while watching the linux side found the web page, which eventually led http://danbenjamin.com/articles/2008/02/ruby-rails-leopard Rubystack package also has a good selection mistake, very la

  • Rails January of dynamic

    More than a month before, Rails 2.2.2 released at the same time, the official immediately issued a statement, saying that Rails 2.3 is under development. See the news, while lamenting on the Rails Core Team, the progress is so compact, at the same ti ...

  • Javascript in the browser environment (seven) XMLHttpRequest

    XMLHttpRequest XMLHttpRequest is a function used to create a http request. XHR initial IE through ActiveX objects are realized. After the beginning of the various browsers support. Now AJAX is the popular XMLHttpRequest object to the adoption of the imple

  • Hibernate's FetchSize and MaxReslut

    Previously EJB to do paging, EJBQL only provide FirstResult and MaxReslut, recently discovered by Hibernate its HQL also provides FetchSize, checked under the API found almost mean, and then found the next line so someone said: setFetchSize is an opt ...

  • SUI-JavaScript rich UI library integration solution

    Introduction: SUI is not a class implementation of the UI, nor is it a standard set of UI markup language, it is just to help the existing UI frameworks (such as Ext, YUI like) to mark the way to the statement and the creation of UI. The names of these ta

  • Msxml2.XMLHTTP version problem

    Projects with an import feature prototype.js of Ajax functionality to update the prompt, the code is very simple, do not have the framework of the background on a jsp to output Text, future use of timers and to update the page Ajax.request encountere ...

  • Answer: After 2.2 upgrade mysql question-driven

    windows installed after the mysql gem error, error is approximately: ArgumentError (NULL pointer given): (eval): 3: in `each_hash ' (eval): 3: in `all_hashes' This is feeling under the windows of the question if the mysql gem version libmysql ...

  • hibernate load delay

    Lazy loading: Lazy loading mechanism is in order to avoid unnecessary performance overhead and put forward the so-called lazy loading is required when the real data at a time when the real implementation of the data load operation. At Hibernate provides f

  • Hibernate query: HQL and Criteria

    HQL query methods generally used in more general way through the query query. Examples are as follows: The Criteria is a more than HQL query object-oriented approach: Created as follows: Criteria crit = session.createCriteria (Object.class); crit.add ...

  • Software development sunflower Baodian [reprint]

    Master the ability to reuse code very familiar with the new API's fast. This is because, he once used a lot of the API, have a lot of reusable code. He knows what is available and what is deficient. He has been using Qt, also used by gtk +, also used

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries