From 1a726bd65450be02b2176bc27edb13c3defc7975 Mon Sep 17 00:00:00 2001 From: gyk <147011991+gyk4j@users.noreply.github.com> Date: Wed, 7 Feb 2024 16:37:56 +0800 Subject: [PATCH] Add Runnable, Future and RunnableFuture interface (#158) --- JShim/JShim.csproj | 4 + JShim/Java/Lang/Runnable.cs | 21 +++++ JShim/Java/Util/Concurrent/Future.cs | 83 ++++++++++++++++++++ JShim/Java/Util/Concurrent/RunnableFuture.cs | 18 +++++ JShim/Javax/Swing/SwingWorker.cs | 2 +- 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 JShim/Java/Lang/Runnable.cs create mode 100644 JShim/Java/Util/Concurrent/Future.cs create mode 100644 JShim/Java/Util/Concurrent/RunnableFuture.cs diff --git a/JShim/JShim.csproj b/JShim/JShim.csproj index 4758291..983f117 100644 --- a/JShim/JShim.csproj +++ b/JShim/JShim.csproj @@ -46,10 +46,13 @@ + + + @@ -78,6 +81,7 @@ + diff --git a/JShim/Java/Lang/Runnable.cs b/JShim/Java/Lang/Runnable.cs new file mode 100644 index 0000000..b68f040 --- /dev/null +++ b/JShim/Java/Lang/Runnable.cs @@ -0,0 +1,21 @@ + +using System; + +namespace Java.Lang +{ + /// + /// Description of Runnable. + /// + public interface Runnable + { + /// + /// When an object implementing interface Runnable is used to create a + /// thread, starting the thread causes the object's run method to be + /// called in that separately executing thread. + /// + /// The general contract of the method run is that it may take any + /// action whatsoever. + /// + void Run(); + } +} diff --git a/JShim/Java/Util/Concurrent/Future.cs b/JShim/Java/Util/Concurrent/Future.cs new file mode 100644 index 0000000..916c170 --- /dev/null +++ b/JShim/Java/Util/Concurrent/Future.cs @@ -0,0 +1,83 @@ + +using System; + +namespace Java.Util.Concurrent +{ + /// + /// A Future represents the result of an asynchronous computation. Methods + /// are provided to check if the computation is complete, to wait for its + /// completion, and to retrieve the result of the computation. The result + /// can only be retrieved using method get when the computation has + /// completed, blocking if necessary until it is ready. Cancellation is + /// performed by the cancel method. Additional methods are provided to + /// determine if the task completed normally or was cancelled. Once a + /// computation has completed, the computation cannot be cancelled. If you + /// would like to use a Future for the sake of cancellability but not + /// provide a usable result, you can declare types of the form Future and + /// return null as a result of the underlying task. + /// + public interface Future + { + /// + /// Attempts to cancel execution of this task. This attempt will fail if + /// the task has already completed, has already been cancelled, or could + /// not be cancelled for some other reason. If successful, and this task + /// has not started when cancel is called, this task should never run. If + /// the task has already started, then the mayInterruptIfRunning + /// parameter determines whether the thread executing this task should + /// be interrupted in an attempt to stop the task. + /// + /// After this method returns, subsequent calls to isDone() will always + /// return true. Subsequent calls to isCancelled() will always return + /// true if this method returned true. + /// + /// + /// true if the thread executing this task should be interrupted; + /// otherwise, in-progress tasks are allowed to complete + /// + /// + /// false if the task could not be cancelled, typically because it has + /// already completed normally; true otherwise + /// + bool Cancel(bool mayInterruptIfRunning); + + /// + /// Waits if necessary for the computation to complete, and then + /// retrieves its result. + /// + /// + /// the computed result + /// + V Get(); + + /// + /// Waits if necessary for at most the given time for the computation to + /// complete, and then retrieves its result, if available. + /// + /// the maximum time to wait + /// the time unit of the timeout argument + /// + /// the computed result + /// + V Get(long timeout, TimeUnit unit); + + /// + /// Returns true if this task was cancelled before it completed + /// normally. + /// + /// + /// true if this task was cancelled before it completed + /// + bool IsCancelled(); + + /// + /// Returns true if this task completed. Completion may be due to normal + /// termination, an exception, or cancellation -- in all of these cases, + /// this method will return true. + /// + /// + /// true if this task completed + /// + bool IsDone(); + } +} diff --git a/JShim/Java/Util/Concurrent/RunnableFuture.cs b/JShim/Java/Util/Concurrent/RunnableFuture.cs new file mode 100644 index 0000000..ee6f6d2 --- /dev/null +++ b/JShim/Java/Util/Concurrent/RunnableFuture.cs @@ -0,0 +1,18 @@ + +using System; +using Java.Lang; + +namespace Java.Util.Concurrent +{ + /// + /// Description of RunnableFuture. + /// + public interface RunnableFuture : Runnable, Future + { + /// + /// Sets this Future to the result of its computation unless it has been + /// cancelled. + /// + new void Run(); + } +} diff --git a/JShim/Javax/Swing/SwingWorker.cs b/JShim/Javax/Swing/SwingWorker.cs index 8999608..c002a47 100644 --- a/JShim/Javax/Swing/SwingWorker.cs +++ b/JShim/Javax/Swing/SwingWorker.cs @@ -12,7 +12,7 @@ namespace Javax.Swing /// /// /// - public abstract class SwingWorker + public abstract class SwingWorker : RunnableFuture { private static readonly ILog log = LogManager.GetLogger(typeof(SwingWorker)); private readonly BackgroundWorker backgroundWorker;