mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-17 07:47:57 -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));
|
task.onDone().fireEvent(new TaskEvent(this, task, false));
|
||||||
taskListeners.forEach(it -> it.onFinished(task));
|
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) {
|
} catch (Exception throwable) {
|
||||||
Throwable resolved = resolveException(throwable);
|
Throwable resolved = resolveException(throwable);
|
||||||
if (resolved instanceof Exception) {
|
if (resolved instanceof Exception) {
|
||||||
@ -268,7 +269,8 @@ public class CancellableTaskExecutor extends TaskExecutor {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Thread.currentThread().setName(task.getName());
|
if (task.getName() != null)
|
||||||
|
Thread.currentThread().setName(task.getName());
|
||||||
if (!executeTask(parentTask, task))
|
if (!executeTask(parentTask, task))
|
||||||
success.set(false);
|
success.set(false);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -19,11 +19,9 @@ package org.jackhuang.hmcl.task;
|
|||||||
|
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import org.jackhuang.hmcl.util.Logging;
|
import org.jackhuang.hmcl.util.Logging;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -50,7 +48,7 @@ public final class Schedulers {
|
|||||||
if (IO_EXECUTOR == null)
|
if (IO_EXECUTOR == null)
|
||||||
IO_EXECUTOR = new ThreadPoolExecutor(6, 6,
|
IO_EXECUTOR = new ThreadPoolExecutor(6, 6,
|
||||||
60L, TimeUnit.SECONDS,
|
60L, TimeUnit.SECONDS,
|
||||||
new SynchronousQueue<>(),
|
new LinkedBlockingQueue<>(),
|
||||||
runnable -> {
|
runnable -> {
|
||||||
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
|
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
|
||||||
thread.setDaemon(true);
|
thread.setDaemon(true);
|
||||||
@ -99,59 +97,8 @@ public final class Schedulers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Future<?> schedule(Executor executor, Runnable command) {
|
public static Future<?> schedule(Executor executor, Runnable command) {
|
||||||
if (executor instanceof ExecutorService) {
|
FutureTask<?> future = new FutureTask<Void>(command, null);
|
||||||
return ((ExecutorService) executor).submit(command);
|
executor.execute(future);
|
||||||
}
|
return future;
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ public abstract class Task<T> {
|
|||||||
* You must initialize stage in preExecute.
|
* You must initialize stage in preExecute.
|
||||||
* @param stage the stage
|
* @param stage the stage
|
||||||
*/
|
*/
|
||||||
public void setStage(String stage) {
|
final void setStage(String stage) {
|
||||||
this.stage = stage;
|
this.stage = stage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -363,18 +363,18 @@ public abstract class Task<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final TaskExecutor cancellableExecutor() {
|
public final TaskExecutor cancellableExecutor() {
|
||||||
return new AsyncTaskExecutor(this);
|
return new CancellableTaskExecutor(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final TaskExecutor cancellableExecutor(boolean start) {
|
public final TaskExecutor cancellableExecutor(boolean start) {
|
||||||
TaskExecutor executor = new AsyncTaskExecutor(this);
|
TaskExecutor executor = new CancellableTaskExecutor(this);
|
||||||
if (start)
|
if (start)
|
||||||
executor.start();
|
executor.start();
|
||||||
return executor;
|
return executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final TaskExecutor cancellableExecutor(TaskListener taskListener) {
|
public final TaskExecutor cancellableExecutor(TaskListener taskListener) {
|
||||||
TaskExecutor executor = new AsyncTaskExecutor(this);
|
TaskExecutor executor = new CancellableTaskExecutor(this);
|
||||||
executor.addTaskListener(taskListener);
|
executor.addTaskListener(taskListener);
|
||||||
return executor;
|
return executor;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user