From 2eae61d7acc6686750a83fc661b2cf6de0fe3b03 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 11 Jun 2024 18:45:17 -0700 Subject: [PATCH] Change Shell.EXECUTOR type from ExecutorService to Executor Remove unnecessary restrictions --- core/src/main/java/com/topjohnwu/superuser/Shell.java | 9 ++++----- .../com/topjohnwu/superuser/internal/JobTask.java | 11 +++++++---- .../topjohnwu/superuser/internal/ShellPipeStream.java | 10 ++++++---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/topjohnwu/superuser/Shell.java b/core/src/main/java/com/topjohnwu/superuser/Shell.java index f142d1a..e4ba280 100644 --- a/core/src/main/java/com/topjohnwu/superuser/Shell.java +++ b/core/src/main/java/com/topjohnwu/superuser/Shell.java @@ -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}. *

- * 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(); /** diff --git a/core/src/main/java/com/topjohnwu/superuser/internal/JobTask.java b/core/src/main/java/com/topjohnwu/superuser/internal/JobTask.java index 4ffde09..4842cf3 100644 --- a/core/src/main/java/com/topjohnwu/superuser/internal/JobTask.java +++ b/core/src/main/java/com/topjohnwu/superuser/internal/JobTask.java @@ -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 outGobbler = EXECUTOR.submit(new StreamGobbler.OUT(stdout, result.out)); - Future errGobbler = EXECUTOR.submit(new StreamGobbler.ERR(stderr, result.err)); + FutureTask outGobbler = + new FutureTask<>(new StreamGobbler.OUT(stdout, result.out)); + FutureTask 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); diff --git a/io/src/main/java/com/topjohnwu/superuser/internal/ShellPipeStream.java b/io/src/main/java/com/topjohnwu/superuser/internal/ShellPipeStream.java index a56b783..45ad7ec 100644 --- a/io/src/main/java/com/topjohnwu/superuser/internal/ShellPipeStream.java +++ b/io/src/main/java/com/topjohnwu/superuser/internal/ShellPipeStream.java @@ -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 stream = Shell.EXECUTOR.submit(() -> new FileInputStream(fifo)); + FutureTask 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 stream = Shell.EXECUTOR.submit(() -> new FileOutputStream(fifo)); + FutureTask stream = new FutureTask<>(() -> new FileOutputStream(fifo)); + Shell.EXECUTOR.execute(stream); return stream.get(FIFO_TIMEOUT, TimeUnit.MILLISECONDS); } catch (Exception e) { if (e instanceof FileNotFoundException)