接上面java actor模型框架ujavaactor ,上面的示例比较复杂,写一个简单点的示例:
import java.util.HashMap;import java.util.Map;import com.ibm.actor.AbstractActor;import com.ibm.actor.Actor;import com.ibm.actor.DefaultActorManager;import com.ibm.actor.DefaultMessage;import com.ibm.actor.Message;public class TestActor extends AbstractActor { private Actor to; public void setTestActor(Actor sendTo) { this.to = sendTo; } @Override public void activate() { super.activate(); } @Override public void deactivate() { logger.trace("TestActor deactivate: %s", getName()); super.deactivate(); } @Override protected void runBody() { DefaultMessage m = new DefaultMessage("message->from :" + getName(), 20); getManager().send(m, null, this); } @Override protected void loopBody(Message m) { String subject = m.getSubject(); int count = (Integer) m.getData(); if (count > 0) { m = new DefaultMessage("message->from :" + getName(), count - 1); System.out.println(getName() + ":" + subject + " data:" + m.getData()); getManager().send(m, this, to); } } public static void main(String[] args) { DefaultActorManager am = new DefaultActorManager(); Mapactors = new HashMap (); try { Map options = new HashMap (); options.put(DefaultActorManager.ACTOR_THREAD_COUNT, 2); am.initialize(options); TestActor a = (TestActor) am.createActor(TestActor.class, "actor0"); actors.put(a.getName(), a); TestActor b = (TestActor) am.createActor(TestActor.class, "actor1"); actors.put(b.getName(), b); a.setTestActor(b); b.setTestActor(a); for (String key : actors.keySet()) { am.startActor(actors.get(key)); } Thread.sleep(10000000); // am.terminateAndWait(); } catch (Exception e) { } }}
输出:
actor0:message->from :actor0 data:19actor1:message->from :actor1 data:19actor1:message->from :actor0 data:18actor0:message->from :actor1 data:18actor1:message->from :actor0 data:17actor0:message->from :actor1 data:17actor0:message->from :actor1 data:16actor1:message->from :actor0 data:16actor1:message->from :actor0 data:15actor0:message->from :actor1 data:15actor0:message->from :actor1 data:14actor1:message->from :actor0 data:14actor0:message->from :actor1 data:13actor1:message->from :actor0 data:13actor0:message->from :actor1 data:12actor1:message->from :actor0 data:12actor1:message->from :actor0 data:11actor0:message->from :actor1 data:11actor0:message->from :actor1 data:10actor1:message->from :actor0 data:10actor0:message->from :actor1 data:9actor1:message->from :actor0 data:9actor1:message->from :actor0 data:8actor0:message->from :actor1 data:8actor0:message->from :actor1 data:7actor1:message->from :actor0 data:7actor1:message->from :actor0 data:6actor0:message->from :actor1 data:6actor0:message->from :actor1 data:5actor1:message->from :actor0 data:5actor1:message->from :actor0 data:4actor0:message->from :actor1 data:4actor1:message->from :actor0 data:3actor0:message->from :actor1 data:3actor1:message->from :actor0 data:2actor0:message->from :actor1 data:2actor1:message->from :actor0 data:1actor0:message->from :actor1 data:1actor1:message->from :actor0 data:0actor0:message->from :actor1 data:0