知识库--The LifecycleSupport (45)
来源:互联网 发布:淘宝模特拍摄大牌风 编辑:程序博客网 时间:2024/05/19 17:47
public final class LifecycleSupport{ public LifecycleSupport(Lifecycle){ super(); this.lifecycle = lifecycle; } private Lifecycle lifecycle = null; private LifecycleListener listeners[] = new LifecycleListener[0]; public void addLifecycleListener(LifecycleListener listener){ synchronized(listeners){ LifecycleListener results[] = new LifecycleListener[listeners.length + 1]; for(int i = 0;i<listeners.length;i+){ results[i] = listeners[i]; } results[listeners.length] = listener; listeners = results; } } public LifecycleListener[] findLifecycleListeners(){ return listeners; } public fireLifecycleEvent(String type,Object data){ LifecycleEvent event = new LifecycleEvent(lifecycle,type,data);//使用lifecycle作为参数 LifecycleListener interested[] = null; synchronized(listeners){ interested = (LifecycleListener)listeners.clone(); } for(int i = 0;i < interested.length;i++){ interested[i].lifecycleEvent(event); } } public void removeLifecycleListener(LifecycleListener listener){ synchronized(listeners){ int n = -1; for(int i = 0;i<listeners.length;i++){ if(listeners[i] == listener){ n = i; break; } } if(n < 0){ return; } LifecycleListener results[] = new LifecycleListener[listeners.length - 1]; int j = 0; for(int i = 0; i < listeners.length;i++){ if(i != n) results[j++]=listeners[i]; } listeners = results; } } }
When a listener is added in the addLifecycleListener method, a new array is created with the size of the number of elements in the old array plus one. Then, all elements from the old array are copied to the new array and the new listener is added. When a listener is removed in the removeLifecycleListener method, a new array is also created with the size of the number of elements in the old array minus one. Then, all elements except the one removed are copied to the new array.
The fireLifecycleEvent method fires a lifecyle event. First, it clones the listeners array. Then, it calls the lifecycleEvent method of each member, passing the triggered event.
A component implementing Lifecycle can use the LifecycleSupport class. For example, the SimpleContext class in the application accompanying this chapter declares the following variable:
protected LifecycleSupport lifecycle = new LifecycleSupport(this);
To add a lifecycle listener, the SimpleContext calls the addLifecycleListener method of the LifecycleSupport class:
public void addLifecycleListener(LifecycleListener listener){ lifecycle.addLifecycleListener(listener); }
To remove a lifecycle listener, the SimpleContext class calls the removeLifecycleListener method of the LifecycleSupport class.
public void removeLifecycleListener(LifecycleListener listener){
lifecycle.removeLifecycleListener(listener);
}
To fire an event, the SimpleContext class calls the fireLifecycleEvent method of the LifecycleSupport class, such as in the following line of code:
lifecycle.fireLifecycleEvent(START_EVENT,null);
- 知识库--The LifecycleSupport (45)
- 知识库--The Loader Interface(47)
- 知识库--The Manager Interface(49)
- 知识库--The Connector Interface
- 知识库--The HttpProcessor Class
- 知识库--Parsing the Connection
- 知识库--The Pipeline Interface
- 知识库--Concurrency+Determining the Number of Threads(76)
- 知识库--Parsing the Request & Headers
- 知识库--The Simple Container Application
- 知识库 --allocate the servlet(54)
- 知识库--面向对象编程的缺陷(The Deficiency of the Object Model)(118)
- 知识库--Lifecycle (44)
- 知识库--SimpleLoader (46)
- 知识库--Logger (46)
- 知识库--WebappLoader(47)
- 知识库--WebappClassLoader(47)
- 知识库--Session(47)
- heroku 上部署node.js的几个注意点
- 学习之Dragger2依赖注解
- 2016年12月9日学习总结---- 函数参数和返回值
- unity直接关闭自己的activity
- 基于workerman的实时推送,摒弃ajax轮询
- 知识库--The LifecycleSupport (45)
- 实时查询引擎 - 介绍总结
- 浅析JNDI
- win10+ubuntu系统迁移到SSD
- tomcat7查看后台管理怎么设置用户名和密码
- java中if暗含的性能优化玄机
- Android Studio更新后出现Error:Cause: failed to find target with hash string 'android-23' 错误
- 【51nod】1008 N的阶乘 mod P
- VS2013编译时出现无法打开包括文件: “SDKDDKVer.h”: No such file or director