Xamarin - Java.Lang.Thread 与 System.Threading.Thread - 使用哪一个?

Xamarin - Java.Lang.Thread vs System.Threading.Thread - which one to use?

在我最近从事的 Xamarin 项目中,我可以看到开发人员使用了 Java.Lang.Thread 以及 System.Threading.Thread(对于非常相似的操作——例如在后台加载数据)。

我只是想知道在线程 class 继承自 IDisposable 的 Xamarin 项目中使用 Java.Lang.Thread 背后的原因是什么 所以只有确保它也被正确处理才有意义(这意味着它是一个额外的代码)。

什么在 Xamarin 生态系统中表现更好?
它们基本上可以互换吗?

我只想提一下 System.Threading.Thread 派生自 CriticalFinalizerObject, which is a safe one implementation of the standard Disposable pattern from Microsoft:

Ensures that all finalization code in derived classes is marked as critical.

而不是 Java.Lang.Thread 只实现了 IDisposable 接口。

这些 class 之间的另一个区别是它们在定义上略有不同。让我们看看文档:

Java.Lang.Thread Class:

A Thread is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Each application has at least one thread running when it is started, the main thread, in the main ThreadGroup. The runtime keeps its own threads in the system thread group.

链接的文章说线程组的使用被认为是过时的。

System.Threading.Thread Class

A process can create one or more threads to execute a portion of the program code associated with the process. Use a ThreadStart delegate or the ParameterizedThreadStart delegate to specify the program code executed by a thread. The ParameterizedThreadStart delegate allows you to pass data to the thread procedure.

它也来自_Thread:

Exposes the Thread class to unmanaged code.

此接口用于从非托管代码访问托管classes,不应从托管代码调用。

因此,正如我所见,java-one 线程是 JVM 内部的逻辑结构,而不是 c#-one 是非托管消费线程。我建议您衡量一个实施和另一个实施的资源,然后决定相应地使用哪个 class。

如果您的许多代码是基于.NET 端的,我建议使用CLR 实现。在其他情况下,我认为您应该使用 JVM 实现。