0

In C#, when I do

lock(this)
{
    //do something
}

it's shorthand for:

Monitor.Enter(this);
try
{
    //do something
}
finally
{
    Monitor.Exit(this);
}

What about java? Is

synchronized(this) {
    //do something
}

also short for something? Is there also some equivalent of the System.Threading.Monitor class in java?

Pavel
  • 1
  • 3
  • 17
  • 51

3 Answers3

2

In Java there are:

  • a synchronized block:

    synchronized(this) {
       //do something
    }
    
  • a synchronized method:

    public synchronized void someMethod() {
       //do something
    }
    
  • methods wait(), notify() and notifyAll() that every object has. They must be called when a thread has owned this object's monitor, in other words, from synchronized methods or blocks:

    public synchronized void waitForOtherTHreadToSetTheCondition() {
        while(!condition) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
    }
    
    public synchronized setCondition() {
        condition = true;
        notifyAll();
    }
    
  • a Lock interface and its subclasses:

        Lock l = ...;
        l.lock();
        try {
            // access the resource protected by this lock
        } finally {
            l.unlock();
        }
    
  • many other helpful classes from the java.util.concurrent package, such as Semaphore, SynchronousQueue, CountDownLatch

Answering your question "Is synchronized also shorthand for something?"

No. And there is no equivalent of System.Threading.Monitor in Java.

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
1

Unlike .NET, Java's monitor locks are implemented with bytecode instructions rather than an API. Where you would see a call to Monitor.[Try]Enter or Monitor.Exit in .NET IL, you would instead see a monitorenter or monitorexit instruction in Java bytecode (similarly guarded with try/finally).

The only way to utilize the built-in monitor lock programmatically is via the synchronized keyword or via sun.misc.Unsafe, which has monitorEnter and monitorExit methods. However, Unsafe is an internal Oracle API that is not portable, and it is scheduled to be removed in future JDKs. Using it is highly discouraged.

Mike Strobel
  • 25,075
  • 57
  • 69
0

You can find equivalent APIs in Lock and Object

public void lock()

Acquires the lock.

public void unlock()

Attempts to release this lock.

Sample code:

 class X {
   ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() {
       assert !lock.isHeldByCurrentThread();
       lock.lock();
       try {
           // ... method body
       } finally {
           lock.unlock();
       }
   }
 }

Main different: You are calling static methods by passing the object to Monitor class. In Java, you have to execute the methods on instance ( ReentrantLock or Object) and hence passing of object is not required.

Mapping of Monitor in c# with Lock class in java

Enter(this)                =>    lock()
Exit(Object)               =>    unlock()
TryEnter(Object)           =>    tryLock()
TryEnter(Object, TimeSpan) =>    tryLock(long timeout, TimeUnit unit)

Mapping of Monitor in c# with Object class in java

Wait(Object)               =>     wait()
Pulse(object obj)          =>     notify()
PulseAll(Object)           =>     nofityAll()

Regarding other query:

synchronized(this) {
    //do something
}

is similar but not the same.

Related question : Why use a ReentrantLock if one can use synchronized(this)?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211