Concurrency in Kotlin


What concurrency tools does Kotlin have? Only synchronized- and withLock-methods?

How can you implement a basic Producer-Consumer application, which would look something like this in Java (very simplified):

public class Stock {   ...   public synchronized void put(Object o) {   this.items.add(o);   notify();   }   public synchronized Object get() {   if(items.size() == 0) {   try {   wait();   }   catch(InterruptedException e) { }   }   return this.items.poll();   } }

public class Producer extends Thread {   ...   public void run() {   while(true) {   this.stock.put(produce());   }   }   ... }

public class Consumer extends Thread {
  public void run() {
  while(true) {


Or can you even do anything like this in Kotlin yet (without libraries, like Akka)?


Kotlin deliberately has no constructs for concurrency built into the language. We believe this should be handled by libraries. Use Java's locks and the synchronized(x) {} function.


Basically you only need to port 3 classes of LambdaQueues from Java to Kotlin und you have a concurrency framework for Kotlin. If you don't need a big hammer like Akka, LambdaQueues might be a nice solution. LambdaQueues is described very roughly here: -slash- lambdaqueues.It is about queueing lambda expressions à la Apple's Grand Central Dispatch. The code is here: -slash- oplohmann -slash- LambdaQueues I'm planning to make a Kotlin port (have to keep a Java version for my curriculum), but will be too busy for a while for private reasons. This is why I don't provide links for the time being that Google can follow. It is not yet ready to be released "on the Internet". All I can do at the moment is improve the very lacking dokumentation.

If you look at these test classes you can understand how the whole thing works: TrafficLightTest, MemoizingEventCalculatorTest, StartrekTest. To make a version for Kotlin you have to port these classes to Kotlin: AbstractLambdaHandback, LambdaHandback, LambdaHandbackFutureCompletion. That should be it. How to queue Kotlin closures in Java collections is described here:

– Oliver


Good idea indeed.


Coming to think of it you only need to create a Kotlin wrapper class for class LambdaHandback and LambdaHandbackFutureCompletion each. The wrapper class uses LambdaHandback as a delegate and only converts Kotlin closures to JDK8 lambdas and then calls the respective method of the delegate with the converted closure. However, the sad problem remains that JDK8 lambdas are no true closures and not everything that can be done with a true closure such as closures in Kotlin can be done with LambdaQueues.

It would of course be really fun to write the whole think in Kotlin: much better programming means to express designs in code and much more fun. But I guess that HawtDispatch in the end has to stay for the time being, because there is too much knowledge in it of how to do queue scheduling efficiently. I had a look at thread scheduling in Go and the scheduling algorithm is very simple. So writing a similar scheduling algorithm for LambdaQueues or a new project à la HawtDispatch in Kotlin wouldn’t be that difficult, but will still take some time (you constantly have to do performance measurements to compare with HawtDispatch to see whether you are on track with your approach). Contributions and people that would like to get involved are very welcome. If any one is interested in joining in send me a mail (it is mentioned on my github homepage).

Cheers, Oliver


Thank you for the tip, I'll keep that in mind! However, I was originally interested in the structures inside Kotlin language itself that support concurrent programming.