Showing posts with label kilim. Show all posts
Showing posts with label kilim. Show all posts

Saturday, December 10, 2011

Sample code - Kilim

A simple Calculator example -pretty naive but then demonstrates using Actor based model in java

In Kilim a thread must extend Task object and implement the execute method which should throw Pausable exception. Kilim weaver (read it as byte code enhancer) interprets classes containing operations throwing Pausable exception and weaves(enhances) it.

import java.math.RoundingMode;

import kilim.Mailbox;
import kilim.Pausable;
import kilim.Task;

public class Calculator extends Task{

 private Mailbox mailbox;

 public Calculator(Mailbox mailbox) {
  super();
  this.mailbox = mailbox;
 }

 @Override
 public void execute() throws Pausable, Exception {
  while (true) {   
   Calculation calc = mailbox.get(); // blocks
   if (calc.getAnswer() == null) {
    calc.setAnswer(calc.getDividend().divide(calc.getDivisor(), 8, 
      RoundingMode.HALF_UP));    
    System.out.println("Calculator determined answer");
    mailbox.putnb(calc);
   }
   Task.sleep(1000);
  }
 }
}


Thread2 - demonstrates sharing of data between Thread1 using a Mailbox

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Date;
import java.util.Random;

import kilim.Mailbox;
import kilim.Pausable;
import kilim.Task;

public class DeferredDivision extends Task {

 private Mailbox mailbox;

 public DeferredDivision(Mailbox mailbox) {
  super();
  this.mailbox = mailbox;
 }

 @Override
 public void execute() throws Pausable, Exception {
  Random numberGenerator = new Random(new Date().getTime());
  MathContext context = new MathContext(8);
  while (true) {
   System.out.println("I need to know the answer of something");
   mailbox.putnb(new Calculation(
     new BigDecimal(numberGenerator.nextDouble(), context), 
     new BigDecimal(numberGenerator.nextDouble(), context)));
   Task.sleep(1000);
   Calculation answer = mailbox.getnb(); // no block
   if (answer != null && answer.getAnswer() != null) {
    System.out.println("Answer is: " + answer.printAnswer());
   }
  }
 }
}



Using Ant invoke Kilim's weaver(byte code enhancer)

 
  
  
  
  
 



A simple test runner
import kilim.Mailbox;
import kilim.Task;

public class CalculationCooperation {
 public static void main(String[] args) {
  Mailbox sharedMailbox = new Mailbox();

  Task deferred = new DeferredDivision(sharedMailbox);
  Task calculator = new Calculator(sharedMailbox);

  deffered.start();
  calculator.start();

 }
}

Notice how the same Mailbox is shared between threads without a lock or synchronization

Your output will vary — actors are nondeterministic
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.36477377 divided by 0.96829189 is 0.37671881
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.40326269 divided by 0.38055487 is 1.05967029
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.16258913 divided by 0.91854403 is 0.17700744
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.77380722 divided by 0.49075363 is 1.57677330

Sequence Diagram -


Conclusion - As in Scala or Erlang , Actor model can be applied to java.



Also Read
Java development 2.0: Ultra-lightweight Java web services with Gretty
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
5 things you didn't know about java.util.concurrent
The Clean Coder

Friday, December 09, 2011

An actor framework for Java concurrency

Recently my curiosity towards learning something new , bumped me into a framework called Kilim. Ya that's right , not sure how many of you have heard about it.

Goal of this framework - to introduce Actor based concurrency to java ala. making the model similar to Erlang and Scala

Why do we need this switch from thread based model to Actor based model when it comes to concurrency - thread based model depends on using shared memory when it comes to communicating between threads. Actor based model takes a slightly different approach of using Mailboxes to communicate.

So whats the advantage ? - well when coded against Actor based model we don't need to worry about thread locks or use any kind of synchronization blocks. Actors are guaranteed to run on multi-core processors. Since there is no limitation or a dependency on shared memory actors can be scheduled on any core or a processor. Improves the overall performance and the scalability of system.

How to use Kilim? sample code - here








Also Read
Java development 2.0: Ultra-lightweight Java web services with Gretty
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
5 things you didn't know about java.util.concurrent
The Clean Coder