Fork me on GitHub

Tuesday, February 16, 2010

JRuby + Jetlang => Awesome Message Passing Concurrency

It has been quiet sometime since I played around with JRuby.. The last I seriously worked on JRuby was during schnell-jruby age :). And now that celerity has taken the main stage I was back in MRI land for a long time. I recently got a chance to return to JRuby land and play with it for sometime.

Interested in concurrency I tried to work on Software Transactional Memory in JRuby with Akka... But as things didn't go great I was searching for something else interesting to work on and I found Jetlang. Jetlang has been in my interest list of sometime but I never got a chance to work with Java.

Jetlang is based on message passing concurrency. It has the concept of fibers which are independent threads of execution and channels which are conduits attached to fibers. We can pass messages to a fiber through channels and when a message is passed into a channel to a fiber the on receive callback is called with the corresponding parameters..

Following are two examples illustrating the use of fibers and channels for message passing concurrency. The last one is the popular ping pong example used for illustrating message passing across fibers :).








Writing code in Erlang and using Jetlang based message passing concurrency has made me think that... Can Actors (and Message passing fibers) be considered as ideal representations of objects? Isn't this how we want object oriented systems to behave?
Let me know your thoughts...

3 comments:

Jörg W Mittag said...

Can Actors (and Message passing fibers) be considered as ideal representations of objects? Isn't this how we want object oriented systems to behave?

Actors aren't just "ideal representations of objects". The two are one and the same, and there are tons of connections between them.

Erlang's concurrency semantics are based on Carl Hewitt's Actor Model. (That is historically not quite correct, since the designers of Erlang didn't know about the Actor Model until more than a decade after they had already designed Erlang, but it is close enough for now.) Carl Hewitt's Actor Model is based on Smalltalk-71's message-driven execution semantics (in Smalltalk-71 every object is its own independent process, objects collaborate by sending asynchronous messages to each other and the execution of the entire system is exclusively driven by messages – sound familiar?). Alan Kay showed Smalltalk-71 to Carl Hewitt, who realized that while Alan Kay was only interested in how to structure complex software systems, this model would also be very good for concurrency. So, he simplified the semantics (which he deemed much too complex) and called it the Actor Model. Smalltalk-71 in turn is based on Carl Hewitt's PLANNER, so there is also a connection there.

That's connection number 1.

Carl Hewitt's PLANNER was not only the inspiration for the message-driven execution semantics of Smalltalk-71, it was also the predecessor of Prolog, which in turn was the inspiration for Erlang (and also the implementation language of the first couple of Erlang interpreters).

Connection number 2.

Alan Kay based his language design on biological cells. (He minored in microbiology.) Joe Armstrong and Co. based their language design on the needs of the telco industry: massive concurrency, resiliency, and so on. Those are exactly the same things that complex biological organisms face, which is why mother nature came up with a very similar design to Erlang and since Alan Kay based OO on biology, it is again no surprise that both came to the same design.

Connection number 3.

See also Dr. Alan Kay on the Meaning of “Object-Oriented Programming” and Alan Kay on Messaging.

Joel said...

Good article. However, I feel like it doesn't quite do justice to all jetlang can provide in jruby. I wrote up some of my thoughts of my blog; take a look http://www.blog.wordaligned.com/2010/02/17/concurrency-models-in-jruby-using-jetlang/

Sai Venkatakrishnan said...

@Jörg W Mittag - Thanks for the wonderful explanation and Alan Kay's work on objects. I agree that Actors and objects were conceived from the idea of messaging..

@Joel - Read the examples in your post and cool examples with Jetrlang. I used Jetlang as pretty raw without much of abstraction over the library.. Thank you..