Concurrent Hash Map in Kotlin
Asked Answered
P

4

7

Is it possible to implement a concurrent hash map purely in Kotlin (without Java dependency)? I am new to Kotlin and it looks like there is no obvious API available in kotlin.collections.

Pydna answered 2/11, 2020 at 21:49 Comment(0)
C
0

There is no convenient concurrent hash map implemented purely in Kotlin, at least not built into the platform. Someone might have done it in open source, but it would be out of StackOverflow's scope to look for or suggest it. (That said, I wouldn't bet on anyone having done it really correctly.)

True concurrent hash tables are extremely difficult to build and I would not recommend just about anyone make the attempt.

In general, almost certainly the most appropriate thing to do -- especially in the Kotlin multiplatform space -- is to simply wrap a normal MutableMap with a simple mutex (possibly a standard coroutine Mutex). That should generally perform well enough for the environments KMP is likely to be used in.

Cornerstone answered 19/8 at 22:30 Comment(0)
H
0

Try touchlab/Stately. Here's a list of the collections with added thread-safety:

ConcurrentMutableList.kt
ConcurrentMutableMap.kt
ConcurrentMutableSet.kt
Hopping answered 21/8 at 23:12 Comment(0)
P
-1

You can probably convert the source without too many issues. It's freely available, here for example. The concurrency model of Kotlin multiplatform (which I'm guessing is your goal, there's no point in reimplementing it if you only target the JVM) is a bit different than the one Java uses, there are no locks for example. But there's no reason why that would prevent it.

The following resources might also help you with the implementation:

Papain answered 2/11, 2020 at 22:28 Comment(3)
Yes, the goal is Kotlin multiplatform where commonMain does not have access to Java packages. These resources have lot of other multi-threading related concepts. I just need a HashMap where multiple threads can safely insert/remove data. Could you please tell how exactly I can implement this?Pydna
I don't know a lot unfortunately, I haven't even actually used multiplatform. But does it really make sense to have the same implementation for all platforms? JS isn't multithreaded, so a regular map would work just fine. For JVM you already have an implementation. And for native you can do you best to translate the JVM implementation with the resources above. I suggest you also try asking on Kotlin Discussions, you'll certainly get better answers.Papain
Same question, so is there perfect solution for this?Lyophilic
C
-2

You can try:

val emitters: ConcurrentMap<String, Any> = ConcurrentHashMap()

// get
  val obj: Any = emitters[email]

// put:
  emitters[email] = this

// delete
  emitters.remove(email)

Such way, u don't need to add any library to your project

Camisado answered 28/4, 2021 at 17:52 Comment(1)
The whole point of this question was to NOT have any Java dependency. The ConcurrentHashMap you refer to here requires a Java dependency.Transform

© 2022 - 2024 — McMap. All rights reserved.