views:

79

answers:

1

I'm just learning java, and I find that the java docs/books put a strong emphasis on synchronization. I have read quite a bit of c# docs/books, and you just don't see that much emphasis on this topic.

Does c# handle locking/sychronization differently, or do things work differently as a web application (application pools, iis, clr verus containers/jvm/tomcat) ?

+5  A: 

lock and synchronized are broadly the same, although they're implemented somewhat differently - in particular, in C# lock is just syntactic sugar for calls to Monitor.Enter/Exit, whereas in Java there's no library equivalent.

C# doesn't have synchronized methods, although you can use [MethodImpl(MethodImplOptions.Synchronized)] to achieve much the same effect. (I think there are some subtle differences, but they're broadly the same.) I wouldn't suggest using this in either language, however - lock on private locks instead.

The other concurrency library support in .NET and Java is further apart - the primitives of Object.wait/notify/notifyAll and Monitor.Wait/Pulse/PulseAll are similar, but higher level support is fairly different.

The memory models are subtly different - if you're not trying to work without locking they're close enough, but correct lock-free code would be different in .NET and Java.

Not really sure how to answer your web application point...

Jon Skeet
Actually, C# does have synchronized methods. http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions.aspx
SLaks
Not in the language - there's attribute support, admittedly, but it's not part of the language. Will edit.
Jon Skeet