Are there any differences between Java's “synchronized” and C#'s “lock”?

前端 未结 3 1894
栀梦
栀梦 2020-12-10 13:01

Do these two keywords have exactly the same effect, or is there something I should be aware of?

3条回答
  •  清歌不尽
    2020-12-10 13:42

    One interesting difference not covered in the link posted by Keeg: as far as I'm aware, there's no equivalent method calls in Java for .NET's Monitor.Enter and Monitor.Exit, which the C# lock statement boils down to. That means you can't do the equivalent of Monitor.TryEnter either - although of course the java.util.concurrent.locks package (as of 1.5) has a variety of locks which have more features available.

提交回复
热议问题