DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Java Actors with Kilim

Java Actors with Kilim

Alex Miller user avatar by
Alex Miller
·
Jan. 07, 09 · Interview
Like (1)
Save
Tweet
Share
12.52K Views

Join the DZone community and get the full member experience.

Join For Free

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.

import java.util.ArrayList;   
import java.util.List;

import kilim.Mailbox;
import kilim.pausable;

@pausable
public class Ring {

public static void main(String arg[]) {
new Ring().startRing(Integer.parseInt(arg[0]));
}

public void startRing(int n) {
List<NodeActor> nodes = spawnNodes(n, startTimer());
Mailbox<Message> mailbox = connectNodes(n, nodes);
mailbox.putnb(Message.START);

try { Thread.sleep(100000000); } catch(InterruptedException e) {}
}

private TimerActor startTimer() {
TimerActor actor = new TimerActor(new Mailbox<Message>());
actor.start();
return actor;
}

private List<NodeActor> spawnNodes(int n, TimerActor timer) {
System.out.println("constructing nodes");
long startConstructing = System.currentTimeMillis();

List<NodeActor> nodes = new ArrayList<NodeActor>(n+1);
for(int i=0; i<n; i++) {
nodes.add(new NodeActor(i, new Mailbox<Message>(), timer.getInbox()));
nodes.get(i).start();
}

long endConstructing = System.currentTimeMillis();
System.out.println("Took " + (endConstructing-startConstructing) + " ms to construct " + n + " nodes");
return nodes;
}

private Mailbox<Message> connectNodes(int n, List<NodeActor> nodes) {
System.out.println("connecting nodes");
nodes.add(nodes.get(0));
for(int i=0; i<n; i++) {
nodes.get(i).connect(nodes.get(i+1).getInbox());
}
return nodes.get(0).getInbox();
}
}

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:

public class Message {   
public static final Message START = new Message();
public static final Message STOP = new Message();
public static final Message CANCEL = new Message();
}

public class TokenMessage extends Message {
public final int source;
public int value;

public TokenMessage(int source, int value) {
this.source = source;
this.value = value;
}
}

And here’s the node actor translated into a Kilim Task (note the @pausable annotation on the execute() method, which is defined in Task):

import kilim.Mailbox;   
import kilim.Task;
import kilim.pausable;

public class NodeActor extends Task {
private final int nodeId;
private final Mailbox<Message> inbox;
private final Mailbox<Message> timerInbox;
private Mailbox<Message> nextInbox;

public NodeActor(int nodeId, Mailbox<Message> inbox, Mailbox<Message> timerInbox) {
this.nodeId = nodeId;
this.inbox = inbox;
this.timerInbox = timerInbox;
}

public Mailbox<Message> getInbox() {
return this.inbox;
}

public void connect(Mailbox<Message> nextInbox) {
this.nextInbox = nextInbox;
}

@pausable
public void execute() throws Exception {
while(true) {
Message message = inbox.get();
if(message.equals(Message.START)) {
System.out.println(System.currentTimeMillis() + " " + nodeId + ": Starting messages");
timerInbox.putnb(Message.START);
nextInbox.putnb(new TokenMessage(nodeId, 0));
} else if(message.equals(Message.STOP)) {
//System.out.println(System.currentTimeMillis() + " " + nodeId + ": Stopping");
nextInbox.putnb(Message.STOP);
break;
} else if(message instanceof TokenMessage) {
TokenMessage token = (TokenMessage)message;
if(token.source == nodeId) {
int nextVal = token.value+1;
if(nextVal % 10000 == 0) {
System.out.println(System.currentTimeMillis() + " " + nodeId + ": Around ring " + nextVal + " times");
}

if(nextVal == 1000000) {
timerInbox.putnb(Message.STOP);
timerInbox.putnb(Message.CANCEL);
nextInbox.putnb(Message.STOP);
break;
} else {
token.value = nextVal;
nextInbox.putnb(token);
}
} else {
nextInbox.putnb(token);
}
}
}
}
}

And for completeness, here’s the Timer Actor:

import kilim.Mailbox;   
import kilim.Task;
import kilim.pausable;

public class TimerActor extends Task {
private final Mailbox<Message> inbox;
private boolean timing;
private long startTime;

public TimerActor(Mailbox<Message> inbox) {
this.inbox = inbox;
}

public Mailbox<Message> getInbox() {
return this.inbox;
}

@pausable
public void execute() throws Exception {
while(true) {
Message message = inbox.get();
if(message.equals(Message.START) && !timing) {
startTime = System.currentTimeMillis();
timing = true;
} else if(message.equals(Message.STOP) && timing) {
long endTime = System.currentTimeMillis();
System.out.println("Start=" + startTime + " Stop=" + endTime + " Elapsed=" + (endTime-startTime));
timing = false;
} else if(message.equals(Message.CANCEL)) {
break;
}
}
}
}

To compile and weave, you’ll do something like this:

$ export KCP=$KILIM/libs/asm-all-2.2.3.jar:$KILIM/classes  
$ javac -cp $KCP -d bin src/*.java  
$ java -cp $KCP kilim.tools.Weaver -d ./bin ./bin  
$ java -cp $KCPL./bin Ring 100 

And finally the results, here in comparison with Erlang and Scala 2.7.3/JDK 1.6:

LanguageSpawn 100Send 100M messagesSpawn 20k
Erlang R12B0.2 ms77354 ms120 ms
Scala 2.7.310 ms121712 ms315 ms
Kilim3 ms78390 ms192 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.

From http://tech.puredanger.com/

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 5 Data Streaming Trends for 2023
  • Microservices Testing
  • Assessment of Scalability Constraints (and Solutions)
  • What Is Advertised Kafka Address?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: