【发布时间】:2017-01-13 14:43:32
【问题描述】:
我有一些代码,我想知道在多线程环境中我是否会丢失数据...
示例代码如下:
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。步骤如下:
- 初始化 CHM 以保证它包含数据
- 我们有一个提供程序可以实时向我们提供有关该元素的信息,但它不提供过去的数据。当数据来自提供者时,它调用方法“onMessageFromProvider”
- 为了获取项目之前的数据,我们需要进行单独的 HTTP 调用,然后将来自“onMessageFromProvider”的数据与 HTTP 调用的结果合并。完成后,我们可以完全依赖“onMessageFromProvider”所做的任何事情
- 获得 HTTP 调用的结果后,我们将其与来自“onMessageFromProvider”的数据合并,同时应用转换,并打印生成的合并集
- 现在我们删除了映射键,因此我们可以完全依赖“onMessageFromProvider”所做的任何事情
这是否会导致在第 (3) 步运行时可能丢失数据?我该如何解决?为了尽可能少地依赖synchronished,我应该在哪里放置更多代码?
所以恢复,我的目标是永远不会丢失数据,我想确保我的算法能 100% 保证这一点。
很抱歉这篇冗长的帖子,希望它有意义。
更新
根据输入,我正在使用真实示例更新代码,目前如下所示:
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);
}
}
}
【问题讨论】:
标签: java multithreading synchronization