Do these two keywords have exactly the same effect, or is there something I should be aware of?
这两个关键词有完全相同的效果吗?还是有什么我应该注意的?
3 个解决方案
#1
9
According to this site: http://en.csharp-online.net/CSharp_FAQ:_What_is_the_difference_between_CSharp_lock_and_Java_synchronized, C# lock
and Java synchronized
code blocks are "semantically identical", while for methods, Java uses synchronized
while C# uses an attribute: [MethodImpl(MethodImplOptions.Synchronized)]
.
根据这个网站:http://en.csharp . online.net/csharp_faq: _what_is_the_difference between en_csharp_csharp_lock_and_java_synchronized、c# lock和Java synchronized代码块是“语义上相同的”,而对于方法,Java使用synchronized,而c#使用synchronized属性:
#2
5
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.
Keeg发布的链接中没有涉及到一个有趣的区别:据我所知,在Java中没有对。net的监视器进行等效的方法调用。输入和监控。退出,c#锁语句归结为。这就意味着你不能做相当于显示器的工作。TryEnter也可以——尽管当然是java.util.concurrent。锁包(如1.5)有各种锁,这些锁具有更多可用的特性。
#3
-1
I java you don't have to worry about locking public types that you own.
我java你不必担心锁定你所拥有的公共类型。
In .NET, you have to
在。net中,你必须这么做
Updated: this is for types that you own. Locking on public types that you don't own is bad in any language :)
更新:这是针对您所拥有的类型的。锁定不拥有的公共类型在任何语言中都是不好的。
#1
9
According to this site: http://en.csharp-online.net/CSharp_FAQ:_What_is_the_difference_between_CSharp_lock_and_Java_synchronized, C# lock
and Java synchronized
code blocks are "semantically identical", while for methods, Java uses synchronized
while C# uses an attribute: [MethodImpl(MethodImplOptions.Synchronized)]
.
根据这个网站:http://en.csharp . online.net/csharp_faq: _what_is_the_difference between en_csharp_csharp_lock_and_java_synchronized、c# lock和Java synchronized代码块是“语义上相同的”,而对于方法,Java使用synchronized,而c#使用synchronized属性:
#2
5
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.
Keeg发布的链接中没有涉及到一个有趣的区别:据我所知,在Java中没有对。net的监视器进行等效的方法调用。输入和监控。退出,c#锁语句归结为。这就意味着你不能做相当于显示器的工作。TryEnter也可以——尽管当然是java.util.concurrent。锁包(如1.5)有各种锁,这些锁具有更多可用的特性。
#3
-1
I java you don't have to worry about locking public types that you own.
我java你不必担心锁定你所拥有的公共类型。
In .NET, you have to
在。net中,你必须这么做
Updated: this is for types that you own. Locking on public types that you don't own is bad in any language :)
更新:这是针对您所拥有的类型的。锁定不拥有的公共类型在任何语言中都是不好的。