浅谈synchronized应用于类方法和类字面量之上
当调用一个synchronized static函数时,获得的lock将与定义该方法的class的Class对象相关联,而不是与调用方法的那个实例对象相关联。当你对一个class literal调用其synchronized代码块时,获得的也是同样那个lock,与特定Class对象相关联的lock,类的所有实例共享 。
public class TestSyncLock {
public synchronized static void methodOne() { /** 修饰方法 */
/**
* ......
*/
}
public void methodTwo() { /** 修饰object reference */
synchronized (TestSyncLock.class) {
/**
* ......
*/
}
}
public void methodThree() { /** 修饰class literal*/
synchronized (syncObj) { /** syncObj应该为static字面量*/
/**
* ......
*/
}
}
}
methodOne() 和methodTwo()争取的是同一个lock,也就是TestSyncLock的Class object lock。methodOne()通过synchronized的修饰符取得该lock,methodTwo()则通过class literal TestSyncLock.class得到它。如果synchronized施行于instance方法和object references,得到的lock就与前面的不一样了。对于instance函数,取得的lock隶属于其调用者(某个对象),至于同步控制一个对象(synchronized(syncObj)),取得的当然是该对象(syncObj)的lock。
同步控制(1)instance 方法(2)static 方法(3)对象(object) (4)class literals时得到的lock不同
举例1.
public class MyRunnable implements Runnable {
public MyRunnable() {
}
private synchronized void printVal() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
printVal();
}
public static void main(String[] args) {
MyRunnable run = new MyRunnable();
Thread tone = new Thread(run, "threadOne");
tone.start();
MyRunnable run2 = new MyRunnable();
Thread ttwo = new Thread(run2, "threadTwo");
ttwo.start();
}
}
说明:上述代码由于是synchronized instance方法,故使用的是instance对象锁,不同的对象具有不同的对象锁。线程tone和线程ttwo使用的是目标对象run和run2的 run()方法,在run()方法中调用了printVal()方法,由于线程访问的是不同对象(run和run2)的synchronized方法,故两个线程可以并行执行,无需等待对象锁。
如果我们将上面的代码略加修改,即:
private synchronized void printVal() ===> private synchronized static void printVal()
则其输出就不同了,分别输出,只有一个输出完毕后另一个才会输出。此处我们还假设tone先执行(尽管两个线程tone和ttwo的运行顺序是不定的 ),当tone执行synchronized static void printVal()方法的时候,将会获得类TestSyncLock的Class object lock。当后运行的线程ttwo也执行run()方法时,尽管它们两个访问的对象不同,但是线程ttwo执行到方法printVal()的时候,发现其修饰符为synchronized static,此时它也要求类TestSyncLock的Class object lock,但该锁已经被线程tone锁持有,故ttwo只能等待tone执行完毕后释放该锁。
举例2.
public class MyRunnable implements Runnable {
private static byte[] bytes = new byte[0];
@Override
public void run() {
synchronized (bytes) { //注意这里的bytes为static
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
MyRunnable run = new MyRunnable();
Thread tone = new Thread(run, "threadOne");
tone.start();
MyRunnable run2 = new MyRunnable();
Thread ttwo = new Thread(run2, "threadTwo");
ttwo.start();
}
}
说明:经修改后实际和synchronized static void 方法的效果是一样的。
举例3.
public class TestStaticInstanceRunnable implements Runnable {
public synchronized void printMOne() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " printMOne .");
}
}
public synchronized static void printMTwo() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " printMTwo .");
}
}
public void run() {
printMOne();
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
TestStaticInstanceRunnable run = new TestStaticInstanceRunnable();
Thread thread = new Thread(run, "thread");
thread.start();
run.printMTwo();
}
}
说明:在输出时线程main和线程thread交替输出。 线程thread仅仅调用synchronized instance(此处的对象为run)方法printMOne(),在无限循环中打印字符串"printMOne"。主线程main则调用了(此处也是对象run)synchronized static方法printMTwo(),它也进入自身的无限循环中,打印字符串"printMTwo"。
你或许会以为:由于这两个线程(main线程和thread线程)的执行过程中访问的是同一个对象(run)的两个不同synchronized方法,故无法在上述两方法之间交替。这段代码的输出不是字符串"printMOne"就是字符串"printMTwo"一一取决于哪个线程率先进入其synchronized方法并获得lock。
然而,当上述代码执行起来,两个字符串都打印在屏幕上,也就是说,两个方法并发执行。
解析:尽管上述两个方法都声明为synchronized,但它们并非线程安全(thread safe)的,其原因在于一个是synchronized static方法,另一个是synchronized instance方法,因此它们争取的是不同的locks。instance方法printMOne()取得的是TestStaticInstanceRunnable object lock,
static方法printMTwo()取得的是TestStaticInstanceRunnable的Class object lock,是不同的两个locks,彼此互不
影响。
如果希望上述代码在多线程情况下只有一个线程可以得到锁的话,必须为synchronized锁定的是相同的(相同的instance object或Class object),将方法printOne修改成如下形式,则两个方法是用相同的Class object lock。
public void printMOne() {
synchronized(TestStaticInstance.class) {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " printMOne .");
}
}
}
如果需要同步控制这段代码,又该怎么办呢?两种选择可以解决这个问题:
1.同步控制(synchronize)公用资源。
2.同步控制(synchronize)—个特殊的instance变量。
对于1涉及在函数中添加synchronized语句,以便同步控制共享资源。假设两个方法要更新同一个对象,它们就对其进行同步控制。
public class TestSyncLock {
private SyncObj syncObj;
public void methodOne () {
synchronized (syncObj) {
/**
* ......
*/
}
}
public static void methodTwo (TestSyncLock syncLock) {
synchronized (syncLock.syncObj) {
/**
* ......
*/
}
}
}
对于2涉及声明一个local instance 变量,惟一的目的就是对它进行同步控制。
public class TestSyncLock {
private byte [] lock = new byte[0];
public void methodOne () {
synchronized (lock) {
/**
* ......
*/
}
}
public static void methodTwo (TestSyncLock syncLock) {
synchronized (syncLock.lock) {
/**
* ......
*/
}
}
}
由于你只能锁定对象,你使用的local instance 变量必须是个对象。至于为什么使用一个元素个数为0的字节数组,其原因是字节数组比其他任何对象都经济。
上述两种做法中的任何一种都使代码成为线程安全(thread safe)的。同步控制通过instance方法或object reference所取得的lock,完全不同于同步控制通过static方法或class literal 所取得的lock。两个方法被声明为synchronized并不就意味它们具备线程安全性。
public class TestStaticInstanceRunnable implements Runnable {
private byte [] bytes = new byte[0]; //关键在于多线程中锁一定是相同的
public void printMOne() {
synchronized(bytes) {
while (true) {
/** ...... */
}
}
}
public static void printMTwo(TestStaticInstanceRunnable run) {
synchronized(run.bytes) {
while (true) {
/** ...... */
}
}
}
public void run() {
printMOne();
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
TestStaticInstanceRunnable run = new TestStaticInstanceRunnable();
Thread thread = new Thread(run, "thread");
thread.start();
run.printMTwo(run);
}
}
<<To Be Continued>>