我的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;
}
}发布于 2021-09-12 14:39:37
重写客户端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();
...发布于 2021-09-12 15:32:30
每次更新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;
}并在HashMap更新时在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类:
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类:
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: 2https://stackoverflow.com/questions/69152012
复制相似问题