Reputation: 14879
I am reading about Scala’s actors, so say we have something like:
object Worker extends Actor {
def act() {
while(true) {
receive {
case "exit" => {
println("exiting...")
sender ! Exit
}
case s:String if s.startsWith("scp") => {
println("Starting scp")
Thread.sleep(2000)
sender ! Done(s)
}
case s:String => {
println("Starting " + s)
sender ! Done(s)
}
}
}
}
}
(http://www.naildrivin5.com/scalatour/wiki_pages/ActorsAndConcurrency)
What would the equivalent pattern be like with Java? I understand it is much more cumbersome to do this in Java.
Are there any performance implications with Scala’s actors? Sure it is way easier to both implement and understand from what I gather, but curious if there any tradeoffs.
Upvotes: 1
Views: 1002
Reputation: 13535
Scala's actor (not to mix with akka's actor) is effectively a thread with an input queue, and its equivalent can be easily implemented in java:
interface Port<T>{
public void send(T msg);
}
class StringMessage {
String value;
Port sender;
}
class Worker extends Thread implements Port<StringMessage>{
ConcurrentLinkedQueue<StringMessage > q=new ConcurrentLinkedQueue<StringMessage >();
public send(StringMessage m) {
q.put(m);
}
public void run() {
while(true) {
StringMessage msg=q.take();
String s=msg.value;
if (s.equals("exit") {
println("exiting...");
msg.sender.send(Exit);
return;
} else if (s.startsWith("scp") {
println("Starting scp")
Thread.sleep(2000)
msg.sender.send(Exit);
} else {
println("Starting " + s)
msg.sender.send(Done(s));
}
}
}
}
This is only a sketch, to make it workable you have to develop contracts and protocols between communicating threads. Or you can take an existing actor framework for java (there are many). To choose wisely, you have to answer following questions:
should actors be based on Threads or lightweight tasks executing on a thread pool? Threads consume much memory, but allow blocking operations. Most widely known Akka framework uses lightweight tasks.
is actor model enough for you? Classic actor have single input port, more broad dataflow model allow actor node to have several input ports, and the firing occurs when all input ports are not empty. This allow to construct "nested callbacks" as in another question. Java dataflow frameworks are rare, the only opensource library I know is mine df4j. It allows both thread-based and task-based actor nodes, and have subclass Actor with single input.
Upvotes: 4
Reputation: 2832
As someone else mentioned Akka is probably the best candidate, as while it has been written in Scala, it has been done in such a way as to also make it very accessible from Java. As a side note to that the Akka implementation will replace the current implementation in the future.
Also the Scala actor implementation isn't a feature of the language itself, it's just that the standard library includes that implementation.
As far as the performance implications the current Scala implementation isn't that good anyway, so would be a bad example. I can't highly recommend the docs for the Akka one enough however: http://doc.akka.io/docs/akka/2.0.4/
Upvotes: 4
Reputation: 153
Take a look in akka framework. With it you will have the power of Actor Model in Java.
Upvotes: 7