Reputation: 11397
Why are there no keywords for synchronization and concurrency?
So far my research gives me one solution, you wrap some high level classes and use them to handle concurrency.
Given a project in pure Kotlin, what should one do if there is a need for a small, highly optimized component that handles concurrency in a thread-safe manner?
My impression is that Kotlin is an assisting language for Java, to write 90% of the code in Kotlin but have some Java code that is not possible to express with Kotlin.
Is this right? Is this how it was intended to be?
Upvotes: 125
Views: 89064
Reputation: 2154
I think there is a flaw in your base assumption. Although Kotlin is primarily a JVM language, it does run on other platforms. Personally, I haven't worked with another language that supports "synchronized" as a language concept. So the Kotlin devs had the choice of emulating synchronization or relying on other mechanism. They choose the later. Kotlin supports "synchronization" through Mutex and single threading access through a variety of mechanisms, coroutines being the most popular.
As others have stated, there are annotations to expose JVM specific features like synchronization, but that means your Kotlin code will be tied to the JVM. Generally speaking that's probably not a big deal.
I don't have any specific knowledge of how the JVM implements synchronization, but I assume it's using a Mutex under the covers. Mutex is a core part of C, which is what the JVM is written in.
Upvotes: 2
Reputation: 24800
I think suspend
is a concurrency keyword:
fun main() = runBlocking {
launch { doSomething() }
}
suspend fun doSomething() {
delay(1_000)
println("Something was done")
}
Upvotes: 2
Reputation: 86016
Kotlin 1.1 with Coroutines was released and it brings with it async..await
! Read more about it in Kotlin reference docs, Kotlinx Coroutines library and this great in depth Couroutines by Example
Outside of the Kotlin Coroutines, you have these options:
@Synchronized
and @Volatile
annotations which map directly to the same keywords in Javasynchronized
blocks which in Kotlin come from an inline function synchronized()
.Kotlin.concurrent
package and extensions with new functions and also extensions to JDK classes.java.util.concurrent
package such as ConcurrentHashMap
, CountdownLatch
, CyclicBarrier
, Semaphore
, ...java.util.concurrent.locks
package and Kotlin has extensions for a few of these including the cool withLock()
extension function and similar read
/write
extensions for ReentrantReadWriteLock
.java.util.concurrent.atomic
package such as AtomicReference
, AtomicLong
, ...wait
and notify
on objectsYou have everything Java has and more. Your phrase "synchronization and locks" is satisfied by the list above, and then you have even more and without language changes. Any language features would only make it a bit prettier.
So you can have 100% Kotlin code, using the small Kotlin runtime, the JVM runtime from the JDK, and any other JVM library you want to use. No need for Java code, just Java (as-in JVM) libraries.
A quick sample of some features:
class SomethingSyncd {
@Synchronized fun syncFoo() {
}
val myLock = Any()
fun foo() {
synchronized(myLock) {
// ... code
}
}
@Volatile var thing = mapOf(...)
}
Upvotes: 194
Reputation: 11397
I'll answer my own question since actual answer to my question was somewhere deep in kotlin discussions.
What confused me at the time coming from Java was that concurrency keywords were not language keywords they were annotations? to me it seemed strange that important concepts like synchronization were handled through annotations, but now it makes perfect sense. Kotlin is going in the direction of being a platform agnostic language, it's not going to only work on JVM but pretty much anything. So synchronized and volatile were very specific to JVM, they might not be needed in javascript for example.
In a nutshell Kotlin has everything Java has (except package visibility) and much more, a huge difference that no other language has is coroutines. But there is nothing you can write in Java that you cant do in Kotlin... (as far as I am aware)
Upvotes: 14