我有一些代码,我想知道在多线程环境中我是否会丢失数据......
以下是示例代码:
public class TestingJavaThreading {
private final Map<String, Set<String>> data = Maps.newConcurrentMap();
private final HttpClient client;
private final AsyncDataProvider provider;
private final String baseUrl;
// This method is called first...
public void init(String code) {
// We initialise the set to ensure it doesn't throw a null pointer exception or something weird...
data.put(code, Sets.newConcurrentHashSet());
// We tell the provider we're interested in data...
provider.subscribeToDataFrom(code);
// This HTTP call may take long time, and we can't afford losing data, that's why we subscribed beforehand in the previous line...
List<String> elements = client.request(baseUrl + code);
// We add all of the new elements, meanwhile some elements may have been added by "onMessageFromProvider"
data.get(code).addAll(elements);
data.get(code)
.stream()
.map( /* some transformations here, whatever... */)
.forEach(e -> System.out.println(e));
// Now we've printed the merged data from "onMessageFromProvider" + the HTTP call
// We remove the element from the map, so now we only receive data from "onMessageFromProvider"
data.remove(code);
}
public void onMessageFromProvider(String code, String element) {
final Set<String> newSet = data.computeIfPresent(code, (k, v) -> {
v.add(element);
return v;
});
if (newSet == null) {
// Do something else...
}
}
}
基本上,被调用的初始方法是init
。步骤是这样的:
在步骤(3)运行时,这是否会导致数据丢失?我该如何解决?我应该在哪里放置更多代码,以尽可能少地依赖synchronished
?
所以恢复,我的目标是永远不会丢失数据,我想确保我的算法100%保证。
对于loooong帖子感到抱歉,希望它有意义。
更新
根据输入,我用实际样本更新代码,目前看起来像这样:
public class Main {
public static void main(String[] args) throws InterruptedException {
new Main().init("X");
}
public void init(String code) throws InterruptedException {
subscribeToDataFrom(code);
CompletableFuture
.supplyAsync(getDataFromHttpRequest());
}
private Supplier<Set<String>> getDataFromHttpRequest() {
return () -> {
Set<String> resultsToReturn = Sets.newHashSet();
try {
resultsToReturn.add("B");
resultsToReturn.add("C");
resultsToReturn.add("D");
resultsToReturn.add("E");
resultsToReturn.add("F");
Thread.sleep(1000); // Simulate it is a slow request...
} catch (Exception ex) {}
return resultsToReturn;
};
}
private void subscribeToDataFrom(String code) {
Runnable r = () -> {
while (true) {
onMessageFromProvider(code, UUID.randomUUID().toString());
}
};
new Thread(r).start();
new Thread(r).start();
new Thread(r).start();
new Thread(r).start();
new Thread(r).start();
}
public void onMessageFromProvider(String code, String element) {
// Here how do I create the completable future for usage in the previous CompletableFuture????
final Set<String> newSet = data.computeIfPresent(code, (k, v) -> {
v.add(element);
return v;
});
if (newSet == null) {
System.out.println("Ok, now I can do something different with: " + element);
}
}
}
答案 0 :(得分:0)
CompletableFuture类,具有组合和执行不同任务的方法
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html:
例如:
CompletableFuture<String> httpTask = CompletableFuture.supplyAsync(
() -> new HttpTask(httpClient));
Set<String> result = httpTask.thenApply(elements -> //onMessageProvider method
// maybe you can create a Callable class with the logic)
.thenApply(mergedElements -> //remove code).get();
//or try another method
class HttpTask extends Callable<List<String>>{
private HttpClient client;
public HttpTask(HttpClient client){
this.client = client;
}
@Override
public List<String> call() throws Exception {
return client.httpCall(...);
}
}