In Java, Pattern
class is thread-safe all right.
But is Kotlin Regex
(created like "[ABC]+".toRegex()
) thread-safe on the JVM and other runtimes?
The documentation for Regex
points you to java.util.regex.Pattern
for the JVM. So it will inherit Pattern
's concurrency behaviour.
(Note that checking the current implementation isn't enough; JetBrains can and do change their implementations. But if it's specified in the docs, then it's part of the public API and should be fairly reliable.)
The corresponding docs for JavaScript point here, which doesn't mention anything about thread-safety. And that for Native doesn't even have a link. So it's probably not safe to assume anything about them.
In Kotlin toRegex
is just an extension function that creates a Regex
object. On Kotlin/JVM that just calls Pattern.compile(pattern)
, so the underlying code comes from the JDK and is thread-safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With