Presentation is loading. Please wait.

Presentation is loading. Please wait.

Austin Java Users Group developerWorks article – µActor Library BARRY FEIGENBAUM, PH. D. 02/26/13.

Similar presentations


Presentation on theme: "Austin Java Users Group developerWorks article – µActor Library BARRY FEIGENBAUM, PH. D. 02/26/13."— Presentation transcript:

1 Austin Java Users Group developerWorks article – µActor Library BARRY FEIGENBAUM, PH. D. 02/26/13

2 Brief Bio  I have Ph.D. in Computer Engineering  I have been developing software for more than 30 years  I have worked at IBM, Amazon, Dell and others.  I have worked as a developer, designer, team lead, architect and manager  I have worked on business applications, operating systems, tools, web application, etc.  I have been using Java since version 1.0.2.  I am Oracle certified: Java Programmer, Java Developer, Architect for Java Technology  I have written several books and articles; I have numerous US patents

3 Topics  Background  Actor Library Design and Implementation  Sample Actor  Demo

4 Background  Moore’s Law: due to heat dissipation issues moving from faster and faster CPU speed to more copies (i.e., cores) of a CPU per processor  Most computers have multiple cores and the number is increasing over time  Automatic speed up no longer occurring; programs must become much more concurrent to exploit these cores  Traditional imperative languages do this poorly  Hard to code; requires high skill  Near impossible to test correct implementations  Something simpler, such as the Actor Model, is needed

5 Background (cont)  Newer languages offering solutions  Concurrency Libraries  Built-in Language Facilities (ex. Actors; Functional (no side-effects) orientation)  Etc.  JRE provides support but at too low a level  threads  synchronized blocks  java.util.concurrent

6 µActor Library  This presentation concentrates on a Java library for exploiting the Actor model  Wikipedia: The Actor model in computer science is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received.computer science concurrent computation  This library can be used by any Java 6+ application http://www.ibm.com/developerworks/library/j-javaactors/index.html

7 µActor Library (cont)  Lightweight and easy to use  Pure-Java library (no AOP, etc.)  Configurable and dynamic  Key Concepts (interfaces with default implementations)  Actor – unit of execution; processes 1 message at a time  Message – defines an action, has from/to/subject/data/+  ActorManager – manages actors; allocates threads to actors to process messages

8 Actor Runtime Model  Managers own 0+ Actors  Managers manage 0+ Threads  Actors buffer 0+ Messages  Managers send messages to Actors  Actors use Threads to process messages Manager Actor Message * * Thread * Per Message

9 ActorManager  Actor lifetime management  Actor runtime lifecycle management  Message sending services  DefaultActorManager implementation  Adds more API, including thread pool management

10 Actor extends Runnable  Message processing and capacity  Distinct identity, category  Lifecycle, belongs to only one manager  AbstractActor partial implementation  Adds some more services  Multiple levels of subclasses possible

11 Actor Lifecycle 1. New 2. Activate 3. Run 4. Receive (repeated) 5. Deactivate 6. Garbage Activated Run Deactivated Idle Receive Suspended Shutdown New Consumes thread in receive state

12 Message  Message body  Sender, subject, data  Optional but typical  Target supplied via send operation  Added to message by send code  DefaultMessage implementation  Multiple levels of subclasses possible

13 Execution Model  Multiple actors, possibly of different types, cooperate by sending messages to each other.  Sends can fail - count limit exceeded, bad subject, etc.  Each actor has a queue of pending (received) messages.  When a thread is available, the manager dispatches an actor’s receive method passing in the next message.  Receive should be short lived

14 TestActor  A sample actor  Repeats sends count down  Define lifecycle callback methods (for illustration)  Define on start callback public class TestActor extends TestableActor { @Override public void activate() { super.activate(); } @Override public void deactivate() { super.deactivate(); } @Override protected void runBody() { DefaultMessage m = new DefaultMessage("init", 8); getManager().send(m, null, this); }

15 TestActor (cont)  Assume superclass has overridden receive to call loopBody  Process repeat message  Repeat send N-1 times to random actor @Override protected void loopBody(Message m) { String subject = m.getSubject(); if ("repeat".equals(subject)) { int count = (Integer) m.getData(); if (count > 0) { m = new DefaultMessage("repeat", count - 1); String toName = "actor"+ ???; Actor to = actorTest.getTestActors().get(toName) getManager().send(m, this, to); }

16 TestActor (cont)  Process init message  Send count messages to random actors with random delays  Process other messages  Typically signal error or delegate to superclass } else if ("init".equals(subject)) { int count = (Integer) m.getData(); for (int i = 0; i < count; i++) { m = new DefaultMessage("repeat", count); String toName = "actor“ + ???; Actor to = actorTest.getTestActors().get(toName); getManager().send(m, this, to); DefaultMessage dm = new DefaultMessage("repeat", count); dm.setDelayUntil(new Date().getTime() + ???); getManager().send(dm, this, this.getClass().getSimpleName()); } } else { … } }

17 Example Main  Access an actor manager  Create some actors in the common category  Create some actors in the default category  Start all the actors  Keep main alive to allow actors to run  Terminate actor processing DefaultActorManager am = DefaultActorManager.getDefaultInstance(); : Map testActors = new HashMap (); for (int i = 0; i < 2; i++) { Actor a = am.createActor(TestActor.class, "common" + i); a.setCategory("common"); testActors.put(a.getName(), a); } for (int i = 0; i < 5; i++) { Actor a = am.createActor(TestActor.class, "actor" + i); testActors.put(a.getName(), a); } for (String key : testActors.keySet()) { am.startActor(testActors.get(key)); } for (int i = 120; i > 0; i--) { if (i < 10 || i % 10 == 0) System.out.printf("main waiting: %d...%n", i); delay(1000); } : am.terminateAndWait();

18 Visualize running Actors  GUI shows any activity  Controls at top select example and control execution  Green bar shows active threads  Circle shows active actors and messages between them  Small squares show active threads  Lines show messages  Bottom circles show recent history  Right pane show log trace

19 Demo  No Example Selected  Countdown started  Most Busy  Staring to slow down  Slowing more  Near done  Done

20 Online Video of Demo http://www.youtube.com/watch?feature=player_embedded&v=1emMgwyI9Xk

21 Questions


Download ppt "Austin Java Users Group developerWorks article – µActor Library BARRY FEIGENBAUM, PH. D. 02/26/13."

Similar presentations


Ads by Google