fix: java version detection timeout.

This commit is contained in:
huanghongxun 2021-09-29 17:24:32 +08:00
parent 6901228a15
commit 36d1096976

View File

@ -17,6 +17,7 @@
*/ */
package org.jackhuang.hmcl.util.platform; package org.jackhuang.hmcl.util.platform;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
@ -27,8 +28,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.*; import java.nio.file.*;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.*;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -219,11 +219,12 @@ public final class JavaVersion {
return Stream.of(CURRENT_JAVA); return Stream.of(CURRENT_JAVA);
} }
try { try {
LOG.log(Level.FINER, "Looking for Java" + executable); LOG.log(Level.FINER, "Looking for Java:" + executable);
JavaVersion javaVersion = fromExecutable(executable); Future<JavaVersion> future = Schedulers.io().submit(() -> fromExecutable(executable));
JavaVersion javaVersion = future.get(3, TimeUnit.SECONDS);
LOG.log(Level.FINE, "Found Java (" + javaVersion.getVersion() + ") " + javaVersion.getBinary().toString()); LOG.log(Level.FINE, "Found Java (" + javaVersion.getVersion() + ") " + javaVersion.getBinary().toString());
return Stream.of(javaVersion); return Stream.of(javaVersion);
} catch (IOException e) { } catch (ExecutionException | InterruptedException | TimeoutException e) {
LOG.log(Level.WARNING, "Failed to determine Java at " + executable, e); LOG.log(Level.WARNING, "Failed to determine Java at " + executable, e);
return Stream.empty(); return Stream.empty();
} }
@ -318,7 +319,7 @@ public final class JavaVersion {
default: default:
break; break;
} }
return javaExecutables.stream().flatMap(stream -> stream); return javaExecutables.parallelStream().flatMap(stream -> stream);
} }
private static Stream<Path> listDirectory(Path directory) throws IOException { private static Stream<Path> listDirectory(Path directory) throws IOException {