Skip to content

Commit

Permalink
优化性能
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jun 17, 2021
1 parent fb6df48 commit 380d0e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/glavo/checksum/Hasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public int getHashStringLength() {
public final String hashFile(Path file) throws IOException {
MessageDigest md = localMessageDigest.get();
byte[] buffer = localBuffer.get();
int read = 0;
int read;
try (InputStream input = Files.newInputStream(file)) {
do {
read = input.read(buffer);
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/glavo/checksum/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.TreeMap;
import java.util.concurrent.*;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;

public final class Main {

Expand Down Expand Up @@ -294,13 +295,14 @@ private static void verify(Path basePath, BufferedReader reader, Hasher hasher,
});
}
final Hasher finalHasher = hasher;
pool.submit(() -> reader.lines().parallel()
.filter(line -> !line.isEmpty())
pool.submit(() -> reader.lines().collect(Collectors.toList()).stream().parallel()
.forEach(line -> {
if (verifyFile(basePath, line, finalHasher)) {
successCount.add(1);
} else {
failureCount.add(1);
if (!line.isEmpty()) {
if (verifyFile(basePath, line, finalHasher)) {
successCount.add(1);
} else {
failureCount.add(1);
}
}
})).get();

Expand Down

0 comments on commit 380d0e8

Please sign in to comment.