ConcurrentModificationException (Java 2 Platform SE 5.0)

Sat Oct 11 17:16:48 CST 2008发表于Delicious/searchfull
java.util.ConcurrentModificationException异常
阅读全文...
 
本站相关内容:(RSS)

Java ConcurrentModificationException

I was troubleshooting a multi-threaded application for a occasional exception. Run the following code public static void main(String[] args){ Map context = new HashMap (1); context.put(“key_1″, “value_1″); context.put(“key_2″, “value_2″); for (String key: context.keySet()){ System.out.println(context.get(key)); context.remove(key); } } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace;

Holliday Java Quiz: ConcurrentModificationException

Weiqi Gao's Observations (29 reads)

Tomorrow is Thanksgiving in the US. And here is a little Java quiz that you may want to ponder on while the turkey is in the oven. (BTW, I just recently realized I've been pronouncing the word 'oven' wrong all these years. Apparently it's not [oh-vn], but [uh-vn].)

Q: For the following Foo.java, is it ever possible for line 21 to throw a java.util.ConcurrentModificationException?

    1    import java.util.ArrayList; 
    2    import java.util.List; 
    3     
    4    public class Foo { 
    5      private final List<Bar> bars = new ArrayList<Bar>(); 
    6     
    7      public void add(Bar bar) { 
    8        synchronized(bars) { 
    9          bars.add(bar); 
    10       } 
    11     } 
    12    
    13     public void remove(Bar bar) { 
    14       synchronized(bars) { 
    15         bars.remove(bar); 
    16       } 
    17     } 
    18    
    19     public void close() { 
    20       synchronized(bars) { 
    21         for (Bar bar: bars) { 
    22           bar.close(); 
    23         } 
    24         bars.clear(); 
    25       } 
    26     } 
    27   }

Have a happy Thanksgiving.

Holiday Java Quiz: ConcurrentModificationException

Tomorrow is Thanksgiving in the US. And here is a little Java quiz that you may want to ponder on while the turkey is in the oven. (BTW, I just recently realized I've been pronouncing the word 'oven' wrong all these years. Apparently it's not [oh-vn], but [uh-vn].)

Holliday Java Quiz: ConcurrentModificationException

Tomorrow is Thanksgiving in the US. And here is a little Java quiz that you may want to ponder on while the turkey is in the oven. (BTW, I just recently realized I've been pronouncing the word 'oven' wrong all these years. Apparently it's not [oh-vn], but [uh-vn].)

Q: For the following Foo.java, is it ever possible for line 21 to throw a java.util.ConcurrentModificationException?

    1    import java.util.ArrayList; 
    2    import java.util.List; 
    3     
    4    public class Foo { 
    5      private final List<Bar> bars = new ArrayList<Bar>(); 
    6     
    7      public void add(Bar bar) { 
    8        synchronized(bars) { 
    9          bars.add(bar); 
    10       } 
    11     } 
    12    
    13     public void remove(Bar bar) { 
    14       synchronized(bars) { 
    15         bars.remove(bar); 
    16       } 
    17     } 
    18    
    19     public void close() { 
    20       synchronized(bars) { 
    21         for (Bar bar: bars) { 
    22           bar.close(); 
    23         } 
    24         bars.clear(); 
    25       } 
    26     } 
    27   }

Have a happy Thanksgiving.

Java Platform

The Java platform consists of: 1. the Java application programming interfaces (APIs) and 2. the Java Virtual Machine (JVM). A Java program which is written in Java language just can be run in computer which has installed the Java Platform. Java Language defines the syntax and semantics of the Java programming language. It defines basic vocabulary such as primitive types, if/else blocks, the syntax of class declaration, exception syntax, variable scoping rules, and everything else necessa
互联网相关内容:
Java ConcurrentModificationException (2008年04月18日)
Holliday Java Quiz: ConcurrentModificationException (2008年11月27日)
Holiday Java Quiz: ConcurrentModificationException (2008年11月28日)
Holliday Java Quiz: ConcurrentModificationException (2008年11月27日)
Java Platform (2008年04月09日)
How to avoid ConcurrentModificationException? (2008年01月03日)
Enhanced for loop and ConcurrentModificationException (2007年09月03日)
Java Application Platform SDK (2007年09月09日)
The Java Micro Edition Platform (2007年09月14日)