1

In C#, lock keyword is equivalent to Monitor.Enter() and Leave().

In Java, What is the equivalent one to synchronized keyword?

Hyunjik Bae
  • 2,721
  • 2
  • 22
  • 32

1 Answers1

6

The synchronized keyword makes use of JVM internals in its implementation. This is not exposed as a class.

There are additional (more powerful) synchronization primitives in the java.util.concurrent package. These do not use language keywords, but are used as classes, such as ReentrantLock. They in turn make use of sun.misc.Unsafe#park, but you are not supposed to need to know about that.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thank you for your answer. Then, are you using try-finally block instead of synchronized block? – Hyunjik Bae Feb 23 '15 at 03:01
  • Depends. Usually, you would. But these locks are quite flexible. You could conceivably pass them around so that lock acquistion and release don't have to happen in the same stack frame/method. – Thilo Feb 23 '15 at 03:03