jihor
jihor

Reputation: 2599

Kotlin Regex thread-safety

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?

Upvotes: 6

Views: 1397

Answers (2)

gidds
gidds

Reputation: 18577

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.

Upvotes: 5

user2340612
user2340612

Reputation: 10713

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.

Upvotes: 1

Related Questions