Skip to content

Commit a122601

Browse files
committed
add: increase download concurrency. According to https://www.bilibili.com/video/av89055581.
1 parent cfd5191 commit a122601

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/setting/DownloadProviders.java

+7
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
import org.jackhuang.hmcl.download.BMCLAPIDownloadProvider;
2424
import org.jackhuang.hmcl.download.DownloadProvider;
2525
import org.jackhuang.hmcl.download.MojangDownloadProvider;
26+
import org.jackhuang.hmcl.task.Schedulers;
27+
import org.jackhuang.hmcl.ui.FXUtils;
2628

2729
import java.util.Map;
2830
import java.util.Optional;
31+
import java.util.concurrent.ThreadPoolExecutor;
2932

3033
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
3134
import static org.jackhuang.hmcl.util.Lang.mapOf;
@@ -48,6 +51,10 @@ static void init() {
4851
() -> Optional.ofNullable(providersById.get(config().getDownloadType()))
4952
.orElse(providersById.get(DEFAULT_PROVIDER_ID)),
5053
config().downloadTypeProperty());
54+
55+
FXUtils.onChangeAndOperate(downloadProviderProperty, provider -> {
56+
Schedulers.io().setMaximumPoolSize(provider.getConcurrency());
57+
});
5158
}
5259

5360
public static DownloadProvider getDownloadProvider() {

HMCLCore/src/main/java/org/jackhuang/hmcl/download/BMCLAPIDownloadProvider.java

+4
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,8 @@ public String injectURL(String baseURL) {
9090
.replace("https://authlib-injector.yushi.moe", apiRoot + "/mirrors/authlib-injector");
9191
}
9292

93+
@Override
94+
public int getConcurrency() {
95+
return Math.max(Runtime.getRuntime().availableProcessors() * 2, 6);
96+
}
9397
}

HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProvider.java

+6
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ default Stream<String> injectURLs(String[] baseURLs) {
5656
* @throws IllegalArgumentException if the version list does not exist
5757
*/
5858
VersionList<?> getVersionListById(String id);
59+
60+
/**
61+
* The maximum download concurrency that this download provider supports.
62+
* @return the maximum download concurrency.
63+
*/
64+
int getConcurrency();
5965
}

HMCLCore/src/main/java/org/jackhuang/hmcl/download/MojangDownloadProvider.java

+5
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public VersionList<?> getVersionListById(String id) {
7676
public String injectURL(String baseURL) {
7777
return baseURL;
7878
}
79+
80+
@Override
81+
public int getConcurrency() {
82+
return 6;
83+
}
7984
}

HMCLCore/src/main/java/org/jackhuang/hmcl/task/Schedulers.java

+14-20
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222
import org.jetbrains.annotations.NotNull;
2323

2424
import javax.swing.*;
25-
import java.util.concurrent.CountDownLatch;
26-
import java.util.concurrent.ExecutionException;
27-
import java.util.concurrent.Executor;
28-
import java.util.concurrent.ExecutorService;
29-
import java.util.concurrent.Executors;
30-
import java.util.concurrent.Future;
31-
import java.util.concurrent.SynchronousQueue;
32-
import java.util.concurrent.ThreadPoolExecutor;
33-
import java.util.concurrent.TimeUnit;
34-
import java.util.concurrent.TimeoutException;
25+
import java.util.concurrent.*;
3526
import java.util.concurrent.atomic.AtomicReference;
3627

3728
/**
@@ -43,32 +34,35 @@ public final class Schedulers {
4334
private Schedulers() {
4435
}
4536

46-
private static volatile ExecutorService CACHED_EXECUTOR;
37+
private static volatile ThreadPoolExecutor CACHED_EXECUTOR;
4738

48-
public static synchronized Executor newThread() {
39+
public static synchronized ThreadPoolExecutor newThread() {
4940
if (CACHED_EXECUTOR == null)
5041
CACHED_EXECUTOR = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
5142
60, TimeUnit.SECONDS, new SynchronousQueue<>(), Executors.defaultThreadFactory());
5243

5344
return CACHED_EXECUTOR;
5445
}
5546

56-
private static volatile ExecutorService IO_EXECUTOR;
47+
private static volatile ThreadPoolExecutor IO_EXECUTOR;
5748

58-
public static synchronized Executor io() {
49+
public static synchronized ThreadPoolExecutor io() {
5950
if (IO_EXECUTOR == null)
60-
IO_EXECUTOR = Executors.newFixedThreadPool(6, runnable -> {
61-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
62-
thread.setDaemon(true);
63-
return thread;
64-
});
51+
IO_EXECUTOR = new ThreadPoolExecutor(0, 6,
52+
60L, TimeUnit.SECONDS,
53+
new SynchronousQueue<>(),
54+
runnable -> {
55+
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
56+
thread.setDaemon(true);
57+
return thread;
58+
});
6559

6660
return IO_EXECUTOR;
6761
}
6862

6963
private static volatile ExecutorService SINGLE_EXECUTOR;
7064

71-
public static synchronized Executor computation() {
65+
public static synchronized ExecutorService computation() {
7266
if (SINGLE_EXECUTOR == null)
7367
SINGLE_EXECUTOR = Executors.newSingleThreadExecutor(runnable -> {
7468
Thread thread = Executors.defaultThreadFactory().newThread(runnable);

0 commit comments

Comments
 (0)