PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 30, 2022

[FIXED] Why are there no concurrency keywords in Kotlin?

 September 30, 2022     concurrency, java, kotlin     No comments   

Issue

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?


Solution

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(...)
}


Answered By - Jayson Minard
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing