Change Shell.EXECUTOR type from ExecutorService to Executor

Remove unnecessary restrictions
This commit is contained in:
topjohnwu 2024-06-11 18:45:17 -07:00
parent e01d4cbb8d
commit 2eae61d7ac
3 changed files with 17 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2023 John "topjohnwu" Wu
* Copyright 2024 John "topjohnwu" Wu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -37,7 +37,6 @@ import java.lang.annotation.Retention;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -120,13 +119,13 @@ public abstract class Shell implements Closeable {
@interface ConfigFlags {}
/**
* The {@link ExecutorService} that manages all worker threads used in {@code libsu}.
* The {@link Executor} that manages all worker threads used in {@code libsu}.
* <p>
* Note: If the developer decides to replace the default ExecutorService, keep in mind that
* Note: If the developer decides to replace the default Executor, keep in mind that
* each {@code Shell} instance requires at least 3 threads to operate properly.
*/
@NonNull
public static ExecutorService EXECUTOR = Executors.newCachedThreadPool();
public static Executor EXECUTOR = Executors.newCachedThreadPool();
/**

View File

@ -34,7 +34,7 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
abstract class JobTask extends Shell.Job implements Shell.Task, Closeable {
@ -75,10 +75,13 @@ abstract class JobTask extends Shell.Job implements Shell.Task, Closeable {
result.err = list;
}
try {
Future<Integer> outGobbler = EXECUTOR.submit(new StreamGobbler.OUT(stdout, result.out));
Future<Void> errGobbler = EXECUTOR.submit(new StreamGobbler.ERR(stderr, result.err));
FutureTask<Integer> outGobbler =
new FutureTask<>(new StreamGobbler.OUT(stdout, result.out));
FutureTask<Void> errGobbler = new FutureTask<>(new StreamGobbler.ERR(stderr, result.err));
EXECUTOR.execute(outGobbler);
EXECUTOR.execute(errGobbler);
try {
for (ShellInputSource src : sources)
src.serve(stdin);
stdin.write(END_CMD);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2023 John "topjohnwu" Wu
* Copyright 2024 John "topjohnwu" Wu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
class ShellPipeStream {
@ -58,7 +58,8 @@ class ShellPipeStream {
});
// Open the fifo only after the shell request
Future<InputStream> stream = Shell.EXECUTOR.submit(() -> new FileInputStream(fifo));
FutureTask<InputStream> stream = new FutureTask<>(() -> new FileInputStream(fifo));
Shell.EXECUTOR.execute(stream);
return stream.get(FIFO_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (Exception e) {
if (e instanceof FileNotFoundException)
@ -104,7 +105,8 @@ class ShellPipeStream {
});
// Open the fifo only after the shell request
Future<OutputStream> stream = Shell.EXECUTOR.submit(() -> new FileOutputStream(fifo));
FutureTask<OutputStream> stream = new FutureTask<>(() -> new FileOutputStream(fifo));
Shell.EXECUTOR.execute(stream);
return stream.get(FIFO_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (Exception e) {
if (e instanceof FileNotFoundException)