I want to make a test program with CompletableFuture
. I have a class with 2 functions:
public class FutureTextData {
private ConcurrentHashMap<String,Integer> map = new ConcurrentHashMap<>();
private CompletableFuture<Void> futureForText;
public void getCharInText(String text){
futureForText = CompletableFuture.runAsync(() -> {
for (int i = 0; i < text.length()-3; i++) {
map.compute(text.substring(i+1),(key,value) -> value+=1);
map.compute(text.substring(i+2),(key,value) -> value+=1);
map.compute(text.substring(i+3),(key,value) -> value+=1);
}
for(Map.Entry<String ,Integer> entry:map.entrySet()){
if(entry.getKey().length()==3)
System.out.println(entry.getKey());
}
});
}
public void recordCharInText(String outPutFile){
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
File file = new File(outPutFile);
try(BufferedWriter bf = new BufferedWriter(new FileWriter(file))){
for(Map.Entry<String ,Integer> entry:map.entrySet()){
bf.write(entry.getKey() +"<----->" + entry.getValue());
}
}catch (IOException e) {
e.printStackTrace();
}
});
}
}
In getCharInText()
, I want to count the number of certain substrings in the text, and in recordCharInText()
I want to record the current state of the Map.
And when I run the program:
FutureTextData futureTextData = new FutureTextData();
futureTextData.getCharInText(result);
futureTextData.recordCharInText("outFile.txt");
Then everything just completes without errors and everything. i.e. map is not written to the file, and getCharInText()
is not even executed.
Can you tell me what the error is?