博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java actor模型和消息传递简单示例
阅读量:6236 次
发布时间:2019-06-22

本文共 3244 字,大约阅读时间需要 10 分钟。

接上面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();		Map
actors = 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

转载于:https://www.cnblogs.com/secbook/archive/2012/07/31/2655152.html

你可能感兴趣的文章
CentOS7 上部署Haproxy及Nginx 搭建Web群集
查看>>
脚本——如何运用LoadRunner进行http接口测试
查看>>
Nginx 服务语法正确,服务启动失败解决方法
查看>>
定时任务1
查看>>
CRM办公系统为何能掀起移动办公热潮
查看>>
linux代码
查看>>
网络(思科)——静态路由
查看>>
Linux中的VIM编辑
查看>>
一个简单压缩壳的实现
查看>>
Oracle技术之和分区表相关的一点总结(一)
查看>>
HTML(六)------ CSS
查看>>
我理解的== 、equals 、hashcode
查看>>
关于使用Mysql binlog对数据进行恢复的实战
查看>>
iOS两个App应用之间的跳转
查看>>
我的友情链接
查看>>
模仿概念版QQ登录界面(超炫)
查看>>
flex的常用用法二十条
查看>>
CLI命令行接口
查看>>
使用Android自带Gallery组件实现CoverFlow
查看>>
我的友情链接
查看>>