Skip to content

重构 Task - #7344

Draft
Glavo wants to merge 132 commits into
HMCL-dev:mainfrom
Glavo:task
Draft

Glavo wants to merge 132 commits into
HMCL-dev:mainfrom
Glavo:task

Conversation

@Glavo

@Glavo Glavo commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

在 HMCL 中,Task 是异步任务的核心抽象。但是过去 Task API 设计极其复杂与混乱,存在诸多问题:

  1. 过去 Task 的 API 使用了一套声明式回调模型,而且在开发过程中不断为了需求添加新 API,又缺乏文档,导致整个 Task API 过度复杂。
    Task API 的使用者需要了解 preExecute/dependents/postExecute/dependencies 等几个执行阶段的先后顺序和依赖关系,再了解 isRelyingOnDependents()、isRelyingOnDependencies()、isDependentsSucceeded()、isDependenciesSucceeded() 等 API,这样才能明白 Task 的执行模型。
    这套复杂的模型也导致使用 Task API 时很难完善的管理好资源,容易产生资源泄露现象。
  2. Task 对象过于庞大,而且内部还有多个 JavaFX Property,导致和 JavaFX 产生了不必要的耦合。
  3. Task 有一套和 CompletionFuture 较为接近的 API,但存在大量微妙的语义差别,导致开发者和 AI 都容易产生混淆。
  4. Task 的调度机制复杂且沉重,即便是很简单的任务,底层的 AsyncTaskExecutor 也会翻译成多个 CompletionFuture,开销很高。

本 PR 重构了 Task 相关的 API,大幅简化了 API 设计,优化了调度性能,使 Task 更容易开发、阅读、维护。

@github-actions github-actions Bot added the 5000+ label Nov 22, 2026
Glavo added 15 commits November 22, 2026 22:23
Assisted-by: codex:gpt-6.2-astra
Assisted-by: codex:gpt-6.2-astra
Assisted-by: codex:gpt-6.2-astra
Assisted-by: codex:gpt-6.2-astra

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Static analysis found the following issues (max count 10):

/// Creates a task that waits for `future` and returns its result. Cancelling the task handle
/// does not cancel the future or interrupt a running wait. Future cancellation propagates
/// as a [CancellationException].
public static <T extends @Nullable Object> Task<T> fromCompletableFuture(CompletableFuture<T> future) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERROR] [com.puppycrawl.tools.checkstyle.checks.whitespace.SingleSpaceSeparatorCheck]

Use a single space to separate non-whitespace characters.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Static analysis found the following issues (max count 10):

import org.jackhuang.hmcl.task.DownloadException;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskExecutionContext;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERROR] [com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck]

Unused import - org.jackhuang.hmcl.task.TaskExecutionContext.

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERROR] [com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck]

Unused import - org.junit.jupiter.api.Assertions.fail.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Static analysis found the following issues (max count 10):

import org.jackhuang.hmcl.task.DownloadException;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskExecutionContext;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERROR] [com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck]

Unused import - org.jackhuang.hmcl.task.TaskExecutionContext.

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERROR] [com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck]

Unused import - org.junit.jupiter.api.Assertions.fail.

Assisted-by: codex:gpt-6.2-astra

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Static analysis found the following issues (max count 10):

import org.jackhuang.hmcl.task.DownloadException;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskExecutionContext;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERROR] [com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck]

Unused import - org.jackhuang.hmcl.task.TaskExecutionContext.

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERROR] [com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck]

Unused import - org.junit.jupiter.api.Assertions.fail.

Glavo added 23 commits November 25, 2026 20:04
Assisted-by: codex:gpt-6.2-astra
Assisted-by: codex:gpt-6.2-astra

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant