日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Guava关于JAVA中系统组件之间交互通讯(非线程之间通讯)

發布時間:2025/3/20 windows 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Guava关于JAVA中系统组件之间交互通讯(非线程之间通讯) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Guava?EventBus組件 //?Class?is?typically?registered?by?the?container. class?EventBusChangeRecorder?{@Subscribe?public?void?recordCustomerChange(ChangeEvent?e)?{recordChange(e.getChange());} } //?somewhere?during?initialization eventBus.register(new?EventBusChangeRecorder()); //?much?later public?void?changeCustomer()?{ChangeEvent?event?=?getChangeEvent();eventBus.post(event); } 使用方式,定義evenBus實例,通過register方法將需要調用的組件注冊到eventBus中,然后使用eventBus.post(event)方式實現組件交互,event?問一個時間參數,可以理解為,上列中EventBusChangeRecord.recordCustomerChange?的ChangeEvent?參數。

?官網文檔:

EventBus?allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). It is designed exclusively to replace traditional Java in-process event distribution using explicit registration.?It is?not?a general-purpose publish-subscribe system, nor is it intended for interprocess communication.

@Subscribe?定義當調用eventBus.post時,使用EventBusChangeRecorder中的哪個方法進行相應。 EventBus內部機制: 調用EventBus.register?:?將需要調用的組件傳入,如上列中的EventBusChangeRecorder對象實例,EventBus會通過反射以及上面的@Subscribe注釋,得到一個SetMultiMap<Class<?>?,?EventHandler>?的集合。簡單說就是找出需要調用組件的哪個方法,如recordCustomerChange(ChangeEvent?e)

需要注意的是,通過這個方法要求傳入的組件的接口方法有且只能有一個參數。 調用EventBus?的?post(Object?event)方法?:?這里面有兩部,找到任何可能的方法和參數組合將組合放到一個ConcurrentLinkedQueue<EventWithHandler>中,然后循環從Queue中拿出EventWithHandler?進行調用。
就這么簡單~~?,?這所有的事情是在一個線程中完成的,只是EvenBus?為它的成員變量使用了?ThreadLocal?保證線程并發下的問題。

?


轉載于:https://my.oschina.net/u/194300/blog/217883

總結

以上是生活随笔為你收集整理的Guava关于JAVA中系统组件之间交互通讯(非线程之间通讯)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。