• About Me
  • My Slides

WindyGallery's Weblog

~ I am a normal man in quite imperfect little World.

WindyGallery's Weblog

Tag Archives: Fail Fast

Solution for Fail Fast problem {Java Programming}

22 Sunday Mar 2009

Posted by windygallery in Developer

≈ Leave a comment

Tags

Fail Fast, java

The Java containers also have a mechanism to prevent more than one process from modifying the contents of a container. The problem occurs if you’re iterating through a container and some other process steps in and inserts, removes, or changes an object in that container. Maybe you’ve already passed that object, maybe it’s ahead of you, maybe the size of the container shrinks after you call size( )—there are many scenarios for disaster. The Java containers library incorporates a fail-fast mechanism that looks for any changes to the container other than the ones your process is personally responsible for. If it detects that someone else is modifying the container, it immediately produces a ConcurrentModificationException. This is the “fail-fast” aspect—it doesn’t try to detect a problem later on using a more complex algorithm.

It’s quite easy to see the fail-fast mechanism in operation—all you have to do is create an iterator and then add something to the collection that the iterator is pointing to, like this:

//: c09:FailFast.java
// Demonstrates the "fail fast" behavior.
import java.util.*;

public class FailFast {
  public static void main(String[] args) {
    Collection c = new ArrayList();
    Iterator it = c.iterator();
    c.add("An object");
    // Causes an exception:
    String s = (String)it.next();
  }
} ///:~

The exception happens because something is placed in the container after the iterator is acquired from the container. The possibility that two parts of the program could be modifying the same container produces an uncertain state, so the exception notifies you that you should change your code—in this case, acquire the iterator after you have added all the elements to the container.

Note that you cannot benefit from this kind of monitoring when you’re accessing the elements of a List using get( ).

Reference: http://www.cs.waikato.ac.nz/~jcleary/230/TIJ/html/Chap09.htm#Index1036

Subscribe

  • Entries (RSS)
  • Comments (RSS)

Archives

  • August 2017
  • November 2015
  • August 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • May 2012
  • February 2011
  • January 2011
  • August 2010
  • June 2010
  • May 2010
  • April 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • April 2009
  • March 2009
  • November 2008
  • October 2008

Categories

  • Developer
  • Events
  • Games
  • IDeas
  • Love
  • Photos

Meta

  • Register
  • Log in

Create a free website or blog at WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • WindyGallery's Weblog
    • Already have a WordPress.com account? Log in now.
    • WindyGallery's Weblog
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar