Skip to content

Multi Thread remove from ConcurrentHashMap will throw NPE use Scala 2.13.8 #12613

@LuciferYang

Description

@LuciferYang

Reproduction steps

Scala version: 2.13.8

import scala.jdk.CollectionConverters._
    import java.util.concurrent.ConcurrentHashMap
    val map = new ConcurrentHashMap[Int, Integer]().asScala
    val dataRange = 0 until 100
    dataRange.foreach { i => map.put(i, i) }
    val threads = new Array[Thread](10)
    threads.indices.foreach { i =>
      threads(i) = new Thread() {
        override def run(): Unit = {
          dataRange.foreach { i =>
            map.remove(i).foreach(_.intValue())
          }
        }
      }
    }
    threads.foreach(_.start())
    threads.foreach(_.join())

Running the above program with Scala 2.13 may throw the following exceptions

Exception in thread "Thread-3" Exception in thread "Thread-2" java.lang.NullPointerException
  at scala213.test.MultiThreadRemoveSuite$$anon$1.$anonfun$run$2(MultiThreadRemoveSuite.scala:33)
  at scala213.test.MultiThreadRemoveSuite$$anon$1.$anonfun$run$2$adapted(MultiThreadRemoveSuite.scala:33)
  at scala.Option.foreach(Option.scala:437)
  at scala213.test.MultiThreadRemoveSuite$$anon$1.$anonfun$run$1(MultiThreadRemoveSuite.scala:33)
  at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:190)
  at scala213.test.MultiThreadRemoveSuite$$anon$1.run(MultiThreadRemoveSuite.scala:32)
java.lang.NullPointerException
  at scala213.test.MultiThreadRemoveSuite$$anon$1.$anonfun$run$2(MultiThreadRemoveSuite.scala:33)
  at scala213.test.MultiThreadRemoveSuite$$anon$1.$anonfun$run$2$adapted(MultiThreadRemoveSuite.scala:33)
  at scala.Option.foreach(Option.scala:437)
  at scala213.test.MultiThreadRemoveSuite$$anon$1.$anonfun$run$1(MultiThreadRemoveSuite.scala:33)
  at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:190)
  at scala213.test.MultiThreadRemoveSuite$$anon$1.run(MultiThreadRemoveSuite.scala:32)

Problem

NPE should not be thrown

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions