Block level synchronization

倾然丶 夕夏残阳落幕 提交于 2019-12-07 04:25:06

问题


What is the significance of parameter passed to synchronized?

synchronized ( parameter )
{

}

to achieve block level synchronization. Somewhere i saw code like

class test
{
    public static final int lock =1;

    ...

    synchronized(lock){
       ...
    }
}

I don't understand the purpose of this code.

Can anyone give me a better example and/or explain it?


回答1:


It's the reference to lock on. Basically two threads won't execute blocks of code synchronized using the same reference at the same time. As Cletus says, a synchronized method is mostly equivalent to using synchronized (this) inside the method.

I very much hope that the example code you saw wasn't quite like that - you're trying to synchronize on a primitive variable. Synchronization only works on a monitor (via a reference) - even if it were legal code, x would be boxed, which would lead to some very odd behaviour, as some integers will always be boxed to the same references, and others will create a new object each time you box. Fortunately, the Java compiler realises this is a very bad idea, and will give you a compile-time error for the code you've posted.

More reasonable code is:

class Test
{
    private static final Object lock = new Object();

    ...

    synchronized(lock){
       ...
    }
}

I've made the lock private, and changes its type to Object. Whether or not it should be static depends on the situation - basically a static variable is usually used if you want to access/change static data from multiple threads; instance variables are usually used for locks when you want to access/change per-instance data from multiple threads.




回答2:


This:

public synchronized void blah() {
  // do stuff
}

is semantically equivalent to:

public void blah() {
  synchronized (this) {
    // do stuff
  }
}

Some people don't like to use 'this' for synchronization, partly because it's public (in that the instance is visible to external code). That's why you end up with people using private locks:

public class Foo
  private final static String LOCK = new String("LOCK");

  public void blah() {
    synchronized (LOCK) {
      // do stuff
    }
  }
}

The benefit is that LOCK is not visible outside the class plus you can create several locks to deal with more fine-grained locking situations.




回答3:


The purpose of the synchronized statement (ref: here) is to make sure that in a multi-threaded application, only one thread can access a critical data structure at a given time.

For instance, if you let two threads change the same data structure at the same time, the data structure would be corrupted, then you typically protect it with a lock.

In the real world, consider an analog where a public toilet has a key, hanging in a central place. Before you can use the toilet, you need the key, not only to enter, but it's also a guarantee that nobody else will try to enter the same toilet at the same time. They'll have to wait for the key to become available.

This is how such a lock construct works.

The parameter to the synchronized keyword is the key in this case. You lock the key, do what you need to do, then you unlock the key to let others have access to it. If other threads tries to lock the key while it is currently being locked by another thread, that thread will have to wait.




回答4:


I can't explain it as it doesn't compile. You cannot lock a primitive.

Another bad example would be to change it to

public static final Integer lock =1;

This is a bad idea as small auto-boxed primitives are cached and is likely to have strange side effects (if this is done more than once)




回答5:


The object instance that is passed in to synchronized is the "unit of locking". Other threads that execute that try to gain a lock on the same instance will all wait their turn.

The reason people use this rather than the method level synchronized keyword (which locks on the executing class object's instance) is that they might want a finer-grained or different thing to wait for locking on, depending on how their multi-threaded algorithm is designed.



来源:https://stackoverflow.com/questions/875175/block-level-synchronization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!