注意:这里的 EventBus 并不是 Google Guava 工具库中的 EventBus,而是一个单独的 EventBus 项目
github:https://github.com/greenrobot/EventBus
使用 EventBus 仅需三步
- 定义事件消息类
| public static class MessageEvent { } |
- 定义事件消息的处理函数
| @Subscribe(threadMode = ThreadMode.MAIN) |
| public void onMessageEvent(MessageEvent event) { |
| |
| } |
注册或取消注册事件处理函数
| |
| EventBus.getDefault().register(new Object()); |
| |
| EventBus.getDefault().unregister(new Object()); |
- 发送事件消息
| EventBus.getDefault().post(new MessageEvent()); |
测试代码
添加依赖 jar
| <dependency> |
| <groupId>org.greenrobot</groupId> |
| <artifactId>eventbus-java</artifactId> |
| <version>3.3.1</version> |
| </dependency> |
EventBusDemo.java
| import org.greenrobot.eventbus.EventBus; |
| import org.greenrobot.eventbus.EventBusBuilder; |
| import org.greenrobot.eventbus.Subscribe; |
| import org.greenrobot.eventbus.ThreadMode; |
| |
| public class EventBusDemo { |
| |
| private static class MessageEvent { |
| private String message; |
| |
| public MessageEvent(String message) { |
| this.message = message; |
| } |
| |
| public String getMessage() { |
| return message; |
| } |
| |
| public void setMessage(String message) { |
| this.message = message; |
| } |
| } |
| |
| |
| @Subscribe(threadMode = ThreadMode.ASYNC) |
| public void onMessageEvent(MessageEvent msg) { |
| System.out.println("接收消息" + msg.getMessage()); |
| try { |
| Thread.sleep(10000); |
| } catch (InterruptedException e) { |
| throw new RuntimeException(e); |
| } |
| System.out.println("处理消息" + msg.getMessage()); |
| } |
| |
| |
| public static void main(String[] args) { |
| |
| |
| |
| EventBus.getDefault().register(new EventBusDemo()); |
| |
| for (int i=0; i<5; i++) { |
| try { |
| Thread.sleep(1000); |
| } catch (InterruptedException e) { |
| throw new RuntimeException(e); |
| } |
| System.out.println("---------"); |
| EventBus.getDefault().post(new MessageEvent("tttt" + i)); |
| } |
| } |
| } |
执行输出如下:
| --------- |
| 接收消息tttt0 |
| --------- |
| 接收消息tttt1 |
| --------- |
| 接收消息tttt2 |
| --------- |
| 接收消息tttt3 |
| --------- |
| 接收消息tttt4 |
| 处理消息tttt0 |
| 处理消息tttt1 |
| 处理消息tttt2 |
| 处理消息tttt3 |
| 处理消息tttt4 |
可以看到实现了事件消息的并发执行。
@Subscribe 注解的说明
事件消息的处理函数必须使用 @Subscribe
进行注解,该注解源码及属性值的设置说明如下:
| |
| @Documented |
| @Retention(RetentionPolicy.RUNTIME) |
| @Target({ElementType.METHOD}) |
| public @interface Subscribe { |
| /** |
| * 事件消息执行时的线程模式,默认为 POSTING |
| * 1. POSTING 事件处理函数将与事件发布在同一线程内运行。事件传递开销最小,避免了线程切换,适用于已知的可以在很短时间内完成而不需要主线程参与的简单任务。 |
| * 主线程发布事件消息:在主线程进行事件消息的处理 |
| * 子线程发布事件消息:在子线程进行事件消息的处理 |
| * 2. MAIN 如果是在 Android 设备上,在主线程进行事件消息的处理,适用于在很短时间内完成的任务,以避免阻塞主线程。在非 Android 设备上与 POSTING 逻辑一致。 |
| * 主线程发布事件消息:立即进行事件处理,并会阻塞主线程执行 |
| * 子线程发布事件消息:时间在队列中排队等待主线程执行,不会阻塞发布线程 |
| * 3. MAIN_ORDERED 类似 MAIN 模式,事件会被主线程处理,但如果事件消息是在主线程发出,则也会排队等待,不会阻塞主线程执行 |
| * 4. BACKGROUND 事件消息会在子线程执行 |
| * 主线程发布事件消息:将事件消息加入队列,然后等待线程池执行 |
| * 子线程发布事件消息:直接在事件发布的线程执行,并会阻塞发布线程 |
| * 5. ASYNC 无论在哪个线程发布的事件,都会被放入队列,通过线程池执行事件 |
| */ |
| ThreadMode threadMode() default ThreadMode.POSTING; |
| |
| /** |
| * If true, delivers the most recent sticky event (posted with |
| * {@link EventBus#postSticky(Object)}) to this subscriber (if event available). |
| */ |
| boolean sticky() default false; |
| |
| /** Subscriber priority to influence the order of event delivery. |
| * Within the same delivery thread ({@link ThreadMode}), higher priority subscribers will receive events before |
| * others with a lower priority. The default priority is 0. Note: the priority does *NOT* affect the order of |
| * delivery among subscribers with different {@link ThreadMode}s! */ |
| int priority() default 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗