Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8653

foreach index value error

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 2.3.7, 2.4.15
    • Fix Version/s: None
    • Component/s: Groovy Console, groovy-jdk
    • Labels:
      None
    • Environment:
      jdk1.7.0_79
    • Flags:
      Important

      Description

      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()
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              chenxiaojie chenxiaojie
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 12h
                12h
                Remaining:
                Remaining Estimate - 12h
                12h
                Logged:
                Time Spent - Not Specified
                Not Specified