[ https://issues.apache.org/jira/browse/GROOVY-8653?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16519107#comment-16519107 ]
chenxiaojie commented on GROOVY-8653: ------------------------------------- https://issues.apache.org/jira/browse/GROOVY-7409 the reason is similar to this issue. my code is relatively simple, and it is not easy to find this problem. > foreach index value error > ------------------------- > > Key: GROOVY-8653 > URL: https://issues.apache.org/jira/browse/GROOVY-8653 > Project: Groovy > Issue Type: Bug > Components: Groovy Console, groovy-jdk > Affects Versions: 2.3.7, 2.4.15 > Environment: jdk1.7.0_79 > Reporter: chenxiaojie > Priority: Major > Original Estimate: 12h > Remaining Estimate: 12h > > {code:java} > import java.util.concurrent.ConcurrentHashMap > import java.util.concurrent.ExecutorService > import java.util.concurrent.Executors > import java.util.concurrent.TimeUnit > import java.util.concurrent.atomic.AtomicInteger > > ConcurrentHashMap<Integer, AtomicInteger> map = new > ConcurrentHashMap<Integer, AtomicInteger>(10000) > ConcurrentHashMap<Integer, AtomicInteger> map2 = new > ConcurrentHashMap<Integer, AtomicInteger>(10000) > ConcurrentHashMap<Integer, AtomicInteger> map3 = new > ConcurrentHashMap<Integer, AtomicInteger>(10000) > ExecutorService es = Executors.newFixedThreadPool(100) > 1.upto(10000) { > i -> > es.execute { > AtomicInteger count = map.get(i) > if (count == null) { > count = new AtomicInteger(0) > AtomicInteger oldCount = map.putIfAbsent(i, count) > if (oldCount != null) { > count = oldCount > } > } > count.incrementAndGet() > } > } > for (Integer i = 0; i < 10000; i++) { > es.execute { > AtomicInteger count = map2.get(i) > if (count == null) { > count = new AtomicInteger(0) > AtomicInteger oldCount = map2.putIfAbsent(i, count) > if (oldCount != null) { > count = oldCount > } > } > count.incrementAndGet() > } > } > for (Integer i = 0; i < 10000; i++) { > Integer j = i > es.execute { > AtomicInteger count = map3.get(j) > if (count == null) { > count = new AtomicInteger(0) > AtomicInteger oldCount = map3.putIfAbsent(j, count) > if (oldCount != null) { > count = oldCount > } > } > count.incrementAndGet() > } > } > es.shutdown() > es.awaitTermination(1, TimeUnit.MINUTES) > // println 10000 > println map.size() > // println 6583 ??? size is 0-10000 integer > println map2.size() > // println 10000 > println map3.size() > {code} -- This message was sent by Atlassian JIRA (v7.6.3#76005)