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?
First answer:
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.
Second answer:
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.
- synchronized代码块不能修饰基本数据类型的变量
- synchronized代码块修饰基本数据类型的包装类时,如果在同步块中对同步变量进行运算会导致同步失效, 因为内部包含了封箱拆箱动作,包装类引用指向了新的对象
- synchronized代码块修饰String变量时,如果在同步块中对String变量重新赋值会导致同步失效, 因为创建String的实例并不需要new,String变量在重新赋值时引用指向了新的对象
- synchronized(this):相当于修饰普通方法
- synchronized(A.class):相当于修饰static方法
参考:Block level synchronization-stackoverflow
========================
There are several ways to synchronize access to a static variable.
-
Use a synchronized static method. This synchronizes on the class object.
public class Test { private static int count = 0; public static synchronized void incrementCount() { count++; } }
-
Explicitly synchronize on the class object.
public class Test { private static int count = 0; public void incrementCount() { synchronized (Test.class) { count++; } } }
-
Synchronize on some other static object.
public class Test { private static int count = 0; private static final Object countLock = new Object(); public void incrementCount() { synchronized (countLock) { count++; } } }
Method 3 is the best in many cases because the lock object is not exposed outside of your class.
转载自:How to synchronize a static variable among threads running different instances of a class in Java?