重用java.util.Observable类

本文介绍如何使用Java标准库中的Observer/Observable模式实现简单的事件监听器系统。通过示例代码展示了如何创建事件管理器、监听器及事件对象,以及如何触发事件通知。

原文链接:http://www.2cto.com/kf/200609/13555.html 


通常你可以完全开发一套自己的Listener/Event(监听器/事件)系统,但是在Java标准库中已经有了现存的通用解决方案,使用它可以节约大量的时间。

在Java库中,事件和监听器的使用是很普遍的,这些都只是观察者(Observer)模式的例子。Java Uitl包中提供了一个Observable/Observer对,虽然并不是非常强大,但在很多场合下仍然有用。

下面的三个类演示了使用Observer/Observable类的一种方式。

import java.util.*;
public class OEventManager extends Observable {
static public void main(String[ ] args) {
OEventManager mgr = new OEventManager( );
mgr.addObserver( new OListener( ) );
mgr.addObserver( new OListener( ) );
mgr.addObserver( new OListener( ) );
mgr.fireChange("Changed. ");
}
public void fireChange(String msg) {
setChanged( );
notifyObservers( new OEvent(msg));
}
}
class OListener implements Observer {
public void update(Observable o, Object arg) {
System.err.println("Passed "+arg+" by "+o+" to "+this);
}
}
class OEvent extends EventObject {
public OEvent(String msg) {
super(msg);
}
}
一个需要注意的要点是OEvent类把事件消息存储在源中。通常情况下,这儿应该存储的是激发事件的对象,并且事件消息也应该存储在单独的域中,但是这儿这样做是出于简洁的目的。

另一个需要注意的是Observable的setChanged方法的使用,如果Observable没有发生变化,它并不通知观察者;相反,它仅简单地忽略对notifyObservers方法的调用。

下面是例子程序的输出:
Passed OEvent[source=Changed. ] by OEventManager@c9a to OListener@3b63e6
Passed OEvent[source=Changed. ] by OEventManager@c9a to OListener@25cf3e
Passed OEvent[source=Changed. ] by OEventManager@c9a to OListener@48f0cd
因为Observer接口强制了update(Observable, Object)方法签名(signature),所以Observer/Observable类并不能替代你自己定义的Event/Listener类;当该接口可以接受时,它们是一个有用的工具。


[399922.833s] GC(172630) Full GC (Collect on allocation) 1098.00M->975.25M 30876.419ms io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367) at io.reactivex.internal.operators.single.SingleZipArray$ZipCoordinator.innerError(SingleZipArray.java:139) at io.reactivex.internal.operators.single.SingleZipArray$ZipSingleObserver.onError(SingleZipArray.java:175) at io.reactivex.internal.operators.single.SingleTimeout$TimeoutMainObserver.run(SingleTimeout.java:123) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:38) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:26) at java.base@21.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:317) at java.base@21.0.5/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) at java.base@21.0.5/java.lang.Thread.runWith(Thread.java:1596) at java.base@21.0.5/java.lang.Thread.run(Thread.java:1583) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:902) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:878) Caused by: java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. ... 11 more Exception in thread "RxComputationThreadPool-3" io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367) at io.reactivex.internal.operators.single.SingleZipArray$ZipCoordinator.innerError(SingleZipArray.java:139) at io.reactivex.internal.operators.single.SingleZipArray$ZipSingleObserver.onError(SingleZipArray.java:175) at io.reactivex.internal.operators.single.SingleTimeout$TimeoutMainObserver.run(SingleTimeout.java:123) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:38) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:26) at java.base@21.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:317) at java.base@21.0.5/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) at java.base@21.0.5/java.lang.Thread.runWith(Thread.java:1596) at java.base@21.0.5/java.lang.Thread.run(Thread.java:1583) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:902) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:878) Caused by: java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. ... 11 more io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367) at io.reactivex.internal.operators.single.SingleZipArray$ZipCoordinator.innerError(SingleZipArray.java:139) at io.reactivex.internal.operators.single.SingleZipArray$ZipSingleObserver.onError(SingleZipArray.java:175) at io.reactivex.internal.operators.single.SingleTimeout$TimeoutMainObserver.run(SingleTimeout.java:123) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:38) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:26) at java.base@21.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:317) at java.base@21.0.5/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) at java.base@21.0.5/java.lang.Thread.runWith(Thread.java:1596) at java.base@21.0.5/java.lang.Thread.run(Thread.java:1583) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:902) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:878) Caused by: java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. ... 11 more Exception in thread "RxComputationThreadPool-2" io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367) at io.reactivex.internal.operators.single.SingleZipArray$ZipCoordinator.innerError(SingleZipArray.java:139) at io.reactivex.internal.operators.single.SingleZipArray$ZipSingleObserver.onError(SingleZipArray.java:175) at io.reactivex.internal.operators.single.SingleTimeout$TimeoutMainObserver.run(SingleTimeout.java:123) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:38) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:26) at java.base@21.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:317) at java.base@21.0.5/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) at java.base@21.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) at java.base@21.0.5/java.lang.Thread.runWith(Thread.java:1596) at java.base@21.0.5/java.lang.Thread.run(Thread.java:1583) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:902) at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:878) Caused by: java.util.concurrent.TimeoutException: The source did not signal an event for 30000 milliseconds and has been terminated. ... 11 more [399925.643s] GC(172631) Collect on allocation
最新发布
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值