« January Lambda Lounge this Wednesday…. | Home | Another fun night at the Lounge »
Java Actors with Kilim
I finally got around to porting my actor process ring code (Erlang, Scala) to run with the Kilim library tonight.
Kilim is a Java library for lightweight message-passing. The three main things to know about are Tasks, Mailboxes, and @pausable. In Kilim, actors are replaced with Tasks, which are just lightweight runnable objects that serves basically the same role.
Mailboxes are designed for multiple-producer, single-consumer access just like Erlang and Scala actor mailboxes. In Kilim though, Mailbox is just a class and Tasks don’t necessarily have a Mailbox or could even have more than one. Mailboxes are also generic and typed by the kind of message they should receieve. This opens up new ways to compose Tasks and Mailboxes into a broader range of structures. Messages are typically simple (potentially mutable) classes.
Finally, there is an annotation called @pausable. This is used to specify that a method can be paused, continuation-style, during pausable calls; sleep and yield are two provided hooks and the mailbox get / put methods are also pausable. @pausable is also used to mark classes for instrumentation.
That leads me to the compile-time weaver. After you’ve compiled your classes, you need to run a compile-time weaver to modify the bytecode so that the continuation-style pausing is available. At runtime, pausing is used to schedule a large number of actors over a small number of kernel threads.
In all, this is a really interesting set of features that adapts the Erlang/Scala actor model into the statically typed world of Java pretty nicely.
From a usage point of view, it’s kind of a pain. Compile-time weaving sucks rocks as it puts a kink in every tool chain plus there’s nothing happening that couldn’t be done at runtime with a Java agent, as far as I can tell. I also had a lot of time to get the Weaver to run on my first stab at the code. It was far from obvious that the error messages were indicating I had forgotten @pausable on the non-Actor classes. Fortunately, I know enough about ASM to tell what I was missing. And once I got it running I got some weird NoSuchMethodErrors due to incorrectly specifying @pausable on methods that didn’t need it.
These bumps didn’t bother me too much though - this is a new project and I understand that it’s early for that kind of help.
Now to a little code. This is really pretty much a port from the Scala code into Java, which was pretty close. I broke the code (previously in one file) into Ring (the main code), Message, TokenMessage, NodeActor, and TimerActor. You’ll notice there is a LOT more code with the Java version than the Scala or Erlang versions.
It’s very important that the Ring class is marked @pausable or the weaver won’t work. Now the message classes. I defined some immutable singleton messages in Message and a mutable TokenMessage:
And here’s the node actor translated into a Kilim Task (note the @pausable annotation on the execute() method, which is defined in Task):
And for completeness, here’s the Timer Actor:
To compile and weave, you’ll do something like this:
And finally the results, here in comparison with Erlang and Scala 2.7.3/JDK 1.6:
| Language | Spawn 100 | Send 100M messages | Spawn 20k |
|---|---|---|---|
| Erlang R12B | 0.2 ms | 77354 ms | 120 ms |
| Scala 2.7.3 | 10 ms | 121712 ms | 315 ms |
| Kilim | 3 ms | 78390 ms | 192 ms |
So, Kilim demonstrates process creation faster than Scala (but still slower than Erlang) and message-passing in the realm of Erlang and significantly faster than Scala. That’s pretty darn impressive! I’m looking forward to watching where this library goes.
About this entry
You’re currently reading “Java Actors with Kilim,” an entry on Pure Danger Tech
- Published:
- Jan 07 2009 / 1:30 am
- Category:
- Java, concurrency
- Vote:
- Other posts with these tags:

9 Comments
Jump to comment form | comments rss