我的Java程序向客户端的API发出请求,客户端的API将在不规则上返回一个键值对时间间隔(即不是每5秒/10秒,有时是1秒或5秒)。
我已经插入了我自己的代码,这是一个HashMap
,到客户端API代码存储所有的键值对。
我的目标是运行下面用"!! "标记的代码只要Hashmap条件匹配。我不是Java数据同步方面的专家。注意,Thread.Sleep(3000)
不能工作,因为键值对是以不规则的时间间隔更新的。此外,同一键的值也会随着时间的推移而变化。我试着运行下面的程序,它立即运行了标有"!! "的代码块,这不是我想要的。
解决这个问题最有效的方法是什么?
Class Testing{
public static void main(String[] args){
// connect to API...
ClientAPI clientAPI = new ClientAPI();
clientAPI.connect();
// make a request to retrieve an update of a HashMap
clientAPI.requestHashMapUpdate();
// !!! execute the following block of code only if hashmap contains a specific key value pair, i.e. key1, 1
// if hashmap does not contain key or the key-value pair do not match , then wait until the key-value pair are matched and run the code
if(clientAPI.getMyHashMap().containsKey("key1")){
if(clientAPI.getMyHashMap().get("key1") == 1){
System.out.println("Print Me only if key and value are matched !!!!!!!!");
}
}
}
}
Class ClientAPI{
private HashMap<String,Integer> myHashMap;
// Constructor
public clientAPI(){
myHashMap = new HashMap<String,Integer>();
}
// callback function of API's requestHashMapUpdate method
public void updateHashMapKeyValuePair(String key, int value){
System.out.println("Key:" + key + ", Value: " + value);
// code i wrote to update hashmap with key value pair returned from API
myHashMap.put(key,value);
}
// my code to retrieve the latest snapshot of myHashMap
public HashMap<String,Integer> getMyHashMap(){
return myHashMap;
}
}
覆盖您的客户端API(如果它没有关闭扩展):
class MyClientAPI extends ClientAPI {
...
@Override
public void updateHashMapKeyValuePair(String key, int value) {
super.updateHashMapKeyValuePair(key, value);
if("key1".equals(key) && 1 == value)
System.out.println("Print Me only if key and value are matched !!!!!!!!");
}
...
}
,并简单地检查每个新值。
要使用它,只需更改
ClientAPI clientAPI = new MyClientAPI();
解决这个问题的另一种方法是为ClientAPI
类提供注册侦听器的能力
interface UpdateHashMapKeyValuePairListener {
void digestUpdate(String key, int value);
}
class ClientAPI {
...
private List<UpdateHashMapKeyValuePairListener> listeners = new ArrayList();
...
public void registerUpdateListener(UpdateHashMapKeyValuePairListener u) {
listeners.add(u);
}
...
public void updateHashMapKeyValuePair(final String key, final int value) {
listeners.forEach(u -> u.digestUpdate(key, value));
...
}
...
}
那么,任何想知道何时有新值进入的人,只需实现这个接口,例如
...
clientAPI.registerUpdateListener((key, value) -> {
if("key1".equals(key) && 1 == value)
System.out.println("Print Me only if key and value are matched !!!!!!!!");
});
clientAPI.connect();
...
每次更新HashMap时检查其内容。要实现这一点,您可以在Testing
类中注册一个侦听器,以便每次在ClientAPI
类中更新HashMap
时触发该侦听器。
首先定义侦听器的功能接口:
@FunctionalInterface
public interface OnUpdateMapListener {
void onUpdateMap(Map<String, Integer> map);
}
然后在ClientAPI
public class ClientAPI {
private HashMap<String, Integer> myHashMap;
private OnUpdateMapListener onUpdateMapListener;
定义onMapUpdate
方法在ClientAPI
:
public void onMapUpdate(OnUpdateMapListener onUpdateMapListener) {
this.onUpdateMapListener = onUpdateMapListener;
}
并在updateHashMapKeyValuePair
public void updateHashMapKeyValuePair(String key, int value) {
System.out.println("Key:" + key + ", Value: " + value);
// code i wrote to update hashmap with key value pair returned from API
myHashMap.put(key, value);
onUpdateMapListener.onUpdateMap(myHashMap);
}
在main方法中注册监听器并检查映射内容。每次ClientAPI
在updateHashMapKeyValuePair
方法中接收到新的Map内容时都会触发:
clientAPI.onMapUpdate(stringIntegerHashMap -> {
// !!! the following code is executed every time the hashMap is updated
if (clientAPI.getMyHashMap().containsKey("key1")) {
if (clientAPI.getMyHashMap().get("key1") == 1) {
System.out.println("Print Me only if key and value are matched !!!!!!!!");
}
}
});
ClientAPI
class:
public class ClientAPI {
private HashMap<String, Integer> myHashMap;
private OnUpdateMapListener onUpdateMapListener;
// Constructor
public ClientAPI() {
myHashMap = new HashMap<String, Integer>();
}
// callback function of API's requestHashMapUpdate method
public void updateHashMapKeyValuePair(String key, int value) {
System.out.println("Key:" + key + ", Value: " + value);
// code i wrote to update hashmap with key value pair returned from API
myHashMap.put(key, value);
onUpdateMapListener.onUpdateMap(myHashMap);
}
// my code to retrieve the latest snapshot of myHashMap
public HashMap<String, Integer> getMyHashMap() {
return myHashMap;
}
public void requestHashMapUpdate() {
//.....
}
public void onMapUpdate(OnUpdateMapListener onUpdateMapListener) {
this.onUpdateMapListener = onUpdateMapListener;
}
}
Testing
class:
public static void main(String[] args) {
// connect to API...
ClientAPI clientAPI = new ClientAPI();
clientAPI.connect();
// make a request to retrieve an update of a HashMap
clientAPI.requestHashMapUpdate();
clientAPI.onMapUpdate(stringIntegerHashMap -> {
// !!! the following code is executed every time the hashMap is updated
if (clientAPI.getMyHashMap().containsKey("key1")) {
if (clientAPI.getMyHashMap().get("key1") == 1) {
System.out.println("Print Me only if key and value are matched !!!!!!!!");
}
}
});
clientAPI.updateHashMapKeyValuePair("key1", 1);
clientAPI.updateHashMapKeyValuePair("key1", 2);
}
结果:
Key:key1, Value: 1
Print Me only if key and value are matched !!!!!!!!
Key:key1, Value: 2