Irving Cordova
All memory is shared, so access must be synchronized
Use of shared resources that are locked opens the possibility of deadlocks
Bugs hard to reproduce, harder to fix
Locks create contention, which can slow performance.
In short you need to be a wizard
public class StatelessActorDemo {public static void main(String[] args) throws InterruptedException { final MyStatelessActor actor = new MyStatelessActor(); actor.start(); actor.send("Hello"); actor.sendAndWait(10); actor.sendAndContinue(10.0, new MessagingRunnable<String>() { @Override protected void doRun(final String s) { System.out.println("Received a reply " + s); } }); } }
class MyStatelessActor extends DynamicDispatchActor { public void onMessage(final String msg) { System.out.println("Received " + msg); replyIfExists("Thank you"); }
public void onMessage(final Integer msg) { System.out.println("Received a number " + msg); replyIfExists("Thank you"); }
public void onMessage(final Object msg) { System.out.println("Received an object " + msg); replyIfExists("Thank you"); } }
Fiber fiber = new ThreadFiber();
fiber.start();
final CountDownLatch latch = new CountDownLatch(2);
Runnable toRun = new Runnable(){
public void run(){
latch.countDown();
}
};
//enqueue runnable for execution
fiber.execute(toRun);
//repeat to trigger latch a 2nd time
fiber.execute(toRun);
latch.await(10, TimeUnit.SECONDS);
//shutdown thread
fiber.dispose();