Reputation: 2717
I have moved from java to C# and confused about wait() and sleep(). In java, sleep would not remove the lock to an object and after sleep completes threads gows back to work and in case of wait, it does removes lock from the object and allows other threads to access that object for a while.
Is this true in C# as well or there are any differences?
Upvotes: 0
Views: 245
Reputation: 1502716
Yes, this is true in C# as well. For the most part, Thread.sleep
, Object.wait
, Object.notify
, Object.notifyAll
in Java correspond closely to Thread.Sleep
, Monitor.Wait
, Monitor.Pulse
, Monitor.PulseAll
in .NET.
I'd expect a few differences in details like fairness, but the basics work the same way.
However, you should rarely be using these low-level abstractions in modern code, either in Java or C# - in Java, use higher level abstractions in java.util.concurrent
, and in .NET try to use the Task
abstraction if you can (and if you're in .NET 4). In particular, using Task
will prepare you for the feast of asynchrony in C# 5 (and WinRT).
Upvotes: 5