views:

476

answers:

3

I think the question is pretty self-explanatory. Do these two keywords have exactly the same effect, or is there something I should be aware of?

+3  A: 

A quick google gave me this hit:

http://en.csharp-online.net/CSharp_FAQ:_What_is_the_difference_between_CSharp_lock_and_Java_synchronized

Which seems to answer your question, but since it's that easy to find, I'm guessing you're wondering if there's more to it?

Keeg
Nope, "semantically identical" is what I needed to hear... and whether there were any "gotchas", but that page seems to be pretty clear. Thanks. Stackoverflow is my new Google. ;)
Epaga
+2  A: 

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 namespace (as of 1.5) has a variety of locks which have more features available.

Jon Skeet
A: 

I java you don't have to worry about locking public types that you own.

In .NET, you have to

Updated: this is for types that you own. Locking on public types that you don't own is bad in any language :)

bh213
No, it's still a bad idea to lock on public references in Java too. What makes you think it's okay in Java?
Jon Skeet
While an interesting link, it absoltely (and statedly) does not discuss the issue of locking public types. Which do I agree you shouldn't do... it is a shame that is how [MethodImpl] does it...
Marc Gravell
lock on something public and wait for an idiot to put a deadlocking lock on it too.
Andrei Rinea