C#对Java ReentrantLock和Condition的最佳匹配?

时间:2022-06-22 01:18:28

Another cross-language question: can someone tell me what C# Threading constructs best match the Java ReentrantLock and Condition classes? ReentrantLock has lockInterruptibly() and unlock() methods, while Condition has signal() and await() methods. It is this combination that I would like to be able to preserve in the C# code - or something similar... Thanks in advance.

另一个跨语言问题:有人能告诉我C#Threading构造最符合Java ReentrantLock和Condition类吗? ReentrantLock有lockInterruptibly()和unlock()方法,而Condition有signal()和await()方法。正是这种组合,我希望能够保存在C#代码中 - 或类似的东西......在此先感谢。

3 个解决方案

#1


I think what you're looking for is the static Monitor class. I allows for blocking and non-blocking mutex acquisition, as well as condition variable operations. (They call them Pulse, PulseAll and Wait rather than signal and await).

我认为您正在寻找的是静态Monitor类。我允许阻塞和非阻塞互斥锁获取,以及条件变量操作。 (他们称之为Pulse,PulseAll和Wait而不是信号等待)。

#2


The ReaderWriterLock class would also be worth looking into. This is similar to ReentrantReadWriteLock in Java.

ReaderWriterLock类也值得研究。这类似于Java中的ReentrantReadWriteLock。

#3


DISCLAIMER: I don't know these Java classes, I'm taking a stab in the dark here.

免责声明:我不知道这些Java类,我在黑暗中捅了一下。

In C#, you have a lock statement (I think this is something like Java's synchronized statement) which can lock on any object. I suppose using that statement, or Monitor.Enter(obj) and Monitor.Exit(obj) would be a bit like ReentrantLock.

在C#中,你有一个锁定语句(我认为这类似于Java的synchronized语句)可以锁定任何对象。我想使用那个语句,或者Monitor.Enter(obj)和Monitor.Exit(obj)有点像ReentrantLock。

There are two class called ManualResetEvent and AutoResetEvent. These classes have a Wait method, and a Set method, which I suppose is like Condition's signal and await. The difference between these two classes is that a ManualResetEvent stays set (no longer blocking anyone) and must be Reset. And AutoResetEvent is - like its name suggests - reset automatically.

有两个类叫做ManualResetEvent和AutoResetEvent。这些类有一个Wait方法和一个Set方法,我想这就像Condition的signal和await一样。这两个类之间的区别在于ManualResetEvent保持设置(不再阻止任何人)并且必须是Reset。而AutoResetEvent就像它的名字一样 - 自动重置。

#1


I think what you're looking for is the static Monitor class. I allows for blocking and non-blocking mutex acquisition, as well as condition variable operations. (They call them Pulse, PulseAll and Wait rather than signal and await).

我认为您正在寻找的是静态Monitor类。我允许阻塞和非阻塞互斥锁获取,以及条件变量操作。 (他们称之为Pulse,PulseAll和Wait而不是信号等待)。

#2


The ReaderWriterLock class would also be worth looking into. This is similar to ReentrantReadWriteLock in Java.

ReaderWriterLock类也值得研究。这类似于Java中的ReentrantReadWriteLock。

#3


DISCLAIMER: I don't know these Java classes, I'm taking a stab in the dark here.

免责声明:我不知道这些Java类,我在黑暗中捅了一下。

In C#, you have a lock statement (I think this is something like Java's synchronized statement) which can lock on any object. I suppose using that statement, or Monitor.Enter(obj) and Monitor.Exit(obj) would be a bit like ReentrantLock.

在C#中,你有一个锁定语句(我认为这类似于Java的synchronized语句)可以锁定任何对象。我想使用那个语句,或者Monitor.Enter(obj)和Monitor.Exit(obj)有点像ReentrantLock。

There are two class called ManualResetEvent and AutoResetEvent. These classes have a Wait method, and a Set method, which I suppose is like Condition's signal and await. The difference between these two classes is that a ManualResetEvent stays set (no longer blocking anyone) and must be Reset. And AutoResetEvent is - like its name suggests - reset automatically.

有两个类叫做ManualResetEvent和AutoResetEvent。这些类有一个Wait方法和一个Set方法,我想这就像Condition的signal和await一样。这两个类之间的区别在于ManualResetEvent保持设置(不再阻止任何人)并且必须是Reset。而AutoResetEvent就像它的名字一样 - 自动重置。