-1

I'm wondering if there is any difference in runtime at lock vs syncronized. I have learnd that syncronized is a slow operation and outdated at Java. Today I saw the lock at C# and I'm wondering if they are the same and lock is something I "want" to avoid same as in Java or maybe he is much faster and I want to use it...

Thanks!

maor david
  • 187
  • 2
  • 14
  • 2
    http://stackoverflow.com/questions/217707/are-there-any-differences-between-javas-synchronize-and-cs-lock – MichaC Sep 24 '13 at 12:30
  • Since when is `synchronized` outdated? It's not always the right way to implement locking, but it still has its place. – kiheru Sep 24 '13 at 12:35

1 Answers1

2

1 synchronized is not outdated, java.util.concurrent.locks package simply provides extended functions which are not always needed.

2 Locking is done at CPU level and there is no difference between Java and C# in this regard

see http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html

... special instructions, called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others. These memory barriers are usually performed when lock and unlock actions are taken; they are invisible to programmers in a high level language.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275