Pergunta

Attached the code.. what does this mean, synchronized(m)..?? why we should use that..?? What's the difference between synchronized(this) & synchronized(m)..??

class Waiter implements Runnable {

    Message m;

    public Waiter(Message m) {
        this.m = m;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        synchronized (m) {
            try {
                System.out.println("Waiting to get notified at time " +System.currentTimeMillis());
                m.wait();

            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println("Waiter thread notified at time "+System.currentTimeMillis());
            System.out.println("Message processed ");
        }
    }
}
Foi útil?

Solução

The difference between synchronized(this) and synchronized(m) is that by synchronizing on this, you synchronize on the entire instance. So, as you would expect, no body would be able to synchronize on this while you hold the lock.

public synchronized void foo() {
    // Handle shared resource
}

is similar to

public void foo() { 
    synchronize(this) { 
        // Handle shared resource 
    } 
}

By using objects, such as m, you get a more fine grained control over what you want to synchronize and when. But remember that if someone uses foo(), as shown above, it will not stop access to methods that are not synchronized on this:

public void anotherLock() {
    synchronized(m) {
        // Should handle another shared resource
        // otherwise you might get unexpected results
    }
}

While a thread is using foo(), another thread can access anotherLock().

Outras dicas

The java keyword synchronized is used to synchronize different threads by one instance, acting as a mutual exclusive semaphore. Hence, the argument passed to synchronized is the instance which can be owned by one thread exclusively. It is up to you, the programmer, on which instance you like to synchronize your threads.

But it is a good idea to use the resource, which is under racing conditions, or the owning instance of that resource. The later you start a synchronized block and the earlier you leave it, the better your application will scale.

synchronized is used for thread safety. In your case it is used for implementing observer pattern. you want to wait for something to happen on Message object and then only process it so someone will notify on Message object m for which you are waiting (m.wait()).

When you wait on some object you need to take lock on that object for which you always need to put the wait() statement in a synchronized block on wait object. That is why you are using synchronized(m).

You can not replace it with synchronized(this) as you are calling wait() on object m so synchronized should be on m only.

Somewhere in your application you must be calling m.notify() or m.notifyAll() which will resume your wait() on m.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top