在 CompletableFuture class 中使用 join 方法与 get 方法
Use of join method in CompletableFuture class vs get method
我想实现一个功能,将一个大文件分解成多个块并且可以并行处理。
我使用 CompletableFuture 并行执行 运行 任务。
不幸的是,除非我使用加入,否则它不起作用。我很惊讶这种情况正在发生,因为根据文档,get 也是 class 中的阻塞方法,结果是 returns。谁能帮我弄清楚我做错了什么。
//cf.join(); if i uncommnet this everything works
如果我取消注释方法 processChunk 中的上述行,一切正常。我的价值观被打印出来了。但是,如果我删除它,则什么也不会发生。我得到的只是期货已完成但内容未打印的通知。
这是我的输出
i cmpleteddone
i cmpleteddone
i cmpleteddone
i cmpleteddone
i cmpleteddone
我的文本文件是一个非常小的文件(目前)
1212451,London,25000,Blocked
1212452,London,215000,Open
1212453,London,125000,CreditBlocked
1212454,London,251000,DebitBlocked
1212455,London,2500,Open
1212456,London,4000,Closed
1212457,London,25100,Dormant
1212458,London,25010,Open
1212459,London,27000,Open
12124510,London,225000,Open
12124511,London,325000,Open
12124512,London,425000,Open
12124513,London,265000,Open
12124514,London,2577000,Open
12124515,London,2504400,Open
package com.org.java_trial.thread.executors;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ProcessReallyBigFile {
private static final ExecutorService ex = Executors.newFixedThreadPool(5);
private static CompletableFuture<String> processChunk(List<String> lines) {
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
//just for purposes of testing, will be replaced with major function later
lines.stream().forEach(System.out::println);
return "done";
}, ex);
//cf.join(); if i uncommnet this everything works
return cf;
}
private static void readInChunks(String filepath, Integer chunksize) {
List<CompletableFuture<String>> completable = new ArrayList<>();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filepath))) {
String line = null;
List<String> collection = new ArrayList<String>();
int count = 0;
while ((line = reader.readLine()) != null) {
if (count % chunksize == chunksize - 1) {
collection.add(line);
completable.add(processChunk(collection));
collection.clear();
} else {
collection.add(line);
}
count++;
}
// any leftovers
if (collection.size() > 0)
completable.add(processChunk(collection));
} catch (IOException e) {
e.printStackTrace();
}
for (CompletableFuture c : completable) {
c.join();
if (c.isDone() || c.isCompletedExceptionally()) {
try {
System.out.println("i cmpleted" + c.get());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ex.shutdown();
}
public static void main(String[] args) {
String filepath = "C:\somak\eclipse-workspace\java_thingies\java_trial\account_1.csv";
readInChunks(filepath, 3);
}
}
原因是这样的:
collection.clear();
您的控件 returns 返回到没有 .join()
的调用方法,并且您的任务所指的集合已被清除。 luck you ain't getting an exception thrown for concurrent access
。对共享资源的并发访问应该总是同步的。我宁愿这样做:
synchronized(collection) {
collection.clear();
}
和
synchronized(collection) {
lines.stream().forEach(System.out::println);
}
这将在访问 collection
对象时确保线程安全,因为在对其执行任何更新之前,线程需要在实例 collection
上保持监视器。
另外,正如@Holger 所指出的,这样做:
synchronized(collection) {
collection.add(line);
}
我想实现一个功能,将一个大文件分解成多个块并且可以并行处理。
我使用 CompletableFuture 并行执行 运行 任务。 不幸的是,除非我使用加入,否则它不起作用。我很惊讶这种情况正在发生,因为根据文档,get 也是 class 中的阻塞方法,结果是 returns。谁能帮我弄清楚我做错了什么。
//cf.join(); if i uncommnet this everything works
如果我取消注释方法 processChunk 中的上述行,一切正常。我的价值观被打印出来了。但是,如果我删除它,则什么也不会发生。我得到的只是期货已完成但内容未打印的通知。
这是我的输出
i cmpleteddone
i cmpleteddone
i cmpleteddone
i cmpleteddone
i cmpleteddone
我的文本文件是一个非常小的文件(目前)
1212451,London,25000,Blocked
1212452,London,215000,Open
1212453,London,125000,CreditBlocked
1212454,London,251000,DebitBlocked
1212455,London,2500,Open
1212456,London,4000,Closed
1212457,London,25100,Dormant
1212458,London,25010,Open
1212459,London,27000,Open
12124510,London,225000,Open
12124511,London,325000,Open
12124512,London,425000,Open
12124513,London,265000,Open
12124514,London,2577000,Open
12124515,London,2504400,Open
package com.org.java_trial.thread.executors;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ProcessReallyBigFile {
private static final ExecutorService ex = Executors.newFixedThreadPool(5);
private static CompletableFuture<String> processChunk(List<String> lines) {
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
//just for purposes of testing, will be replaced with major function later
lines.stream().forEach(System.out::println);
return "done";
}, ex);
//cf.join(); if i uncommnet this everything works
return cf;
}
private static void readInChunks(String filepath, Integer chunksize) {
List<CompletableFuture<String>> completable = new ArrayList<>();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filepath))) {
String line = null;
List<String> collection = new ArrayList<String>();
int count = 0;
while ((line = reader.readLine()) != null) {
if (count % chunksize == chunksize - 1) {
collection.add(line);
completable.add(processChunk(collection));
collection.clear();
} else {
collection.add(line);
}
count++;
}
// any leftovers
if (collection.size() > 0)
completable.add(processChunk(collection));
} catch (IOException e) {
e.printStackTrace();
}
for (CompletableFuture c : completable) {
c.join();
if (c.isDone() || c.isCompletedExceptionally()) {
try {
System.out.println("i cmpleted" + c.get());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ex.shutdown();
}
public static void main(String[] args) {
String filepath = "C:\somak\eclipse-workspace\java_thingies\java_trial\account_1.csv";
readInChunks(filepath, 3);
}
}
原因是这样的:
collection.clear();
您的控件 returns 返回到没有 .join()
的调用方法,并且您的任务所指的集合已被清除。 luck you ain't getting an exception thrown for concurrent access
。对共享资源的并发访问应该总是同步的。我宁愿这样做:
synchronized(collection) {
collection.clear();
}
和
synchronized(collection) {
lines.stream().forEach(System.out::println);
}
这将在访问 collection
对象时确保线程安全,因为在对其执行任何更新之前,线程需要在实例 collection
上保持监视器。
另外,正如@Holger 所指出的,这样做:
synchronized(collection) {
collection.add(line);
}