mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-16 07:16:27 -04:00
fix: task accidental disappearance
This commit is contained in:
parent
ed5e225cdc
commit
b72d42a09a
@ -193,7 +193,8 @@ public class CancellableTaskExecutor extends TaskExecutor {
|
||||
|
||||
task.onDone().fireEvent(new TaskEvent(this, task, false));
|
||||
taskListeners.forEach(it -> it.onFinished(task));
|
||||
} catch (RejectedExecutionException ignored) {
|
||||
} catch (RejectedExecutionException e) {
|
||||
Logging.LOG.log(Level.SEVERE, "Task rejected: " + task.getName(), e);
|
||||
} catch (Exception throwable) {
|
||||
Throwable resolved = resolveException(throwable);
|
||||
if (resolved instanceof Exception) {
|
||||
@ -268,6 +269,7 @@ public class CancellableTaskExecutor extends TaskExecutor {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
if (task.getName() != null)
|
||||
Thread.currentThread().setName(task.getName());
|
||||
if (!executeTask(parentTask, task))
|
||||
success.set(false);
|
||||
|
@ -19,11 +19,9 @@ package org.jackhuang.hmcl.task;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import org.jackhuang.hmcl.util.Logging;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -50,7 +48,7 @@ public final class Schedulers {
|
||||
if (IO_EXECUTOR == null)
|
||||
IO_EXECUTOR = new ThreadPoolExecutor(6, 6,
|
||||
60L, TimeUnit.SECONDS,
|
||||
new SynchronousQueue<>(),
|
||||
new LinkedBlockingQueue<>(),
|
||||
runnable -> {
|
||||
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
|
||||
thread.setDaemon(true);
|
||||
@ -99,59 +97,8 @@ public final class Schedulers {
|
||||
}
|
||||
|
||||
public static Future<?> schedule(Executor executor, Runnable command) {
|
||||
if (executor instanceof ExecutorService) {
|
||||
return ((ExecutorService) executor).submit(command);
|
||||
}
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
AtomicReference<Exception> wrapper = new AtomicReference<>();
|
||||
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
command.run();
|
||||
} catch (Exception e) {
|
||||
wrapper.set(e);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
Thread.interrupted(); // clear the `interrupted` flag to prevent from interrupting EventDispatch thread.
|
||||
});
|
||||
|
||||
return new Future<Void>() {
|
||||
@Override
|
||||
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDone() {
|
||||
return latch.getCount() == 0;
|
||||
}
|
||||
|
||||
private Void getImpl() throws ExecutionException {
|
||||
Exception e = wrapper.get();
|
||||
if (e != null)
|
||||
throw new ExecutionException(e);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void get() throws InterruptedException, ExecutionException {
|
||||
latch.await();
|
||||
return getImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void get(long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||
if (!latch.await(timeout, unit))
|
||||
throw new TimeoutException();
|
||||
return getImpl();
|
||||
}
|
||||
};
|
||||
FutureTask<?> future = new FutureTask<Void>(command, null);
|
||||
executor.execute(future);
|
||||
return future;
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ public abstract class Task<T> {
|
||||
* You must initialize stage in preExecute.
|
||||
* @param stage the stage
|
||||
*/
|
||||
public void setStage(String stage) {
|
||||
final void setStage(String stage) {
|
||||
this.stage = stage;
|
||||
}
|
||||
|
||||
@ -363,18 +363,18 @@ public abstract class Task<T> {
|
||||
}
|
||||
|
||||
public final TaskExecutor cancellableExecutor() {
|
||||
return new AsyncTaskExecutor(this);
|
||||
return new CancellableTaskExecutor(this);
|
||||
}
|
||||
|
||||
public final TaskExecutor cancellableExecutor(boolean start) {
|
||||
TaskExecutor executor = new AsyncTaskExecutor(this);
|
||||
TaskExecutor executor = new CancellableTaskExecutor(this);
|
||||
if (start)
|
||||
executor.start();
|
||||
return executor;
|
||||
}
|
||||
|
||||
public final TaskExecutor cancellableExecutor(TaskListener taskListener) {
|
||||
TaskExecutor executor = new AsyncTaskExecutor(this);
|
||||
TaskExecutor executor = new CancellableTaskExecutor(this);
|
||||
executor.addTaskListener(taskListener);
|
||||
return executor;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user