Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there are no concurrency keywords in Kotlin?

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 be doing 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?

like image 975
vach Avatar asked Feb 20 '16 07:02

vach


People also ask

What is synchronized keyword in Kotlin?

3. Synchronizing Threads. Unlike Java, Kotlin doesn't have the synchronized keyword. Therefore, to synchronize multiple background operations, you are expected to use either the @Synchronized annotation or the synchronized() standard library inline function.

How do you ensure thread safety in Kotlin?

To conclude, we have seen the main tools provide by Kotlin to ensure thread-safety: The val keyword helping us to define immutable objects. A standard library which provides immutable collections. Some syntactic sugar provided by the data classes , helping us to manipulate immutable objects.

What is thread safe and synchronization?

Thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occurring. Where as Synchronized means only one thread can operate at single time.

What is synchronized in Android?

Synchronized method is a method which can be used by only one thread at a time. Other threads will be waiting until the method will be released. You should have only valid reasons to declare method as synchronized because such method decreases the productivity and performance.


3 Answers

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:

  • the Kovenant library adds Promises to Kotlin
  • the Quasar library provides light-weight threads and continuations
  • @Synchronized and @Volatile annotations which map directly to the same keywords in Java
  • synchronized blocks which in Kotlin come from an inline function synchronized().
  • Kotlin has a Kotlin.concurrent package and extensions with new functions and also extensions to JDK classes.
  • you can access anything in the java.util.concurrent package such as ConcurrentHashMap, CountdownLatch, CyclicBarrier, Semaphore, ...
  • you can access anything in the 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.
  • you can access anything in the java.util.concurrent.atomic package such as AtomicReference, AtomicLong, ...
  • you can use wait and notify on objects

You 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(...) } 
like image 168
Jayson Minard Avatar answered Oct 06 '22 08:10

Jayson Minard


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)

like image 34
vach Avatar answered Oct 06 '22 09:10

vach


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:

  • the Kovenant library adds Promises to Kotlin
  • the Quasar library provides light-weight threads and continuations
  • @Synchronized and @Volatile annotations which map directly to the same keywords in Java
  • synchronized blocks which in Kotlin come from an inline function synchronized().
  • Kotlin has a Kotlin.concurrent package and extensions with new functions and also extensions to JDK classes.
  • you can access anything in the java.util.concurrent package such as ConcurrentHashMap, CountdownLatch, CyclicBarrier, Semaphore, ...
  • you can access anything in the 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.
  • you can access anything in the java.util.concurrent.atomic package such as AtomicReference, AtomicLong, ...
  • you can use wait and notify on objects

You 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(...)
}
like image 43
11 revs, 2 users 99% Avatar answered Oct 06 '22 07:10

11 revs, 2 users 99%