gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:13:25 27 4
gpt4 key购买 nike

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

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

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

最佳答案

我只想提及 System.Threading.Thread 派生自 CriticalFinalizerObject ,这是标准的安全实现 Disposable pattern from Microsoft :

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

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

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

Java.Lang.Thread 类:

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 类

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.

此接口(interface)用于从非托管代码访问托管类,不应从托管代码调用。

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

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

关于c# - Xamarin - Java.Lang.Thread 与 System.Threading.Thread - 使用哪一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37478467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com