Java: 数据同步问题
Java: data synchronization issue
我的 Java 程序向客户端的 API 发出请求,客户端的 API 将 return 键值对 不规则 时间间隔(即不是每 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
class 提供注册侦听器的能力
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
class 中注册一个侦听器,以便在 ClientAPI
class.
中每次更新 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
中的 HashMap 更新时触发侦听器
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
方法中接收新的地图内容时触发:
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
我的 Java 程序向客户端的 API 发出请求,客户端的 API 将 return 键值对 不规则 时间间隔(即不是每 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
class 提供注册侦听器的能力
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
class 中注册一个侦听器,以便在 ClientAPI
class.
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
方法中接收新的地图内容时触发:
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