【问题描述】关于Java中synchronized 用在实例方法和对象方法上面的区别
【问题分析】大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized 来修饰一个方法(实例方法和类方法---注:不知道这样叫准确不准确,大家理解我的意识就行了)。也可以synchronized 来修饰方法里面的一个语句块。
修饰实例方法:
public synchronized void x() throws InterruptedException { for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("x......................."); } }
修饰类方法(static 方法):public static synchronized void staticX() throws InterruptedException { for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("staticX......................."); } }
修饰方法里面语句块:public static void staticX() throws InterruptedException { synchronized (locks) { for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("staticX......................."); } } }
注意:这里不能用synchronized修饰方法外面的语句块(我把他叫做类语句块),虽然我们可以在方法外面定义语句块,这样做会遇到编译错误,这里涉及到了Java里面的对象初始化的部分知识。大概的原因就是synchronized锁住的是对象,当初始化对象的时候,JVM在对象初始化完成之前会调用方法外面的语句块,这个时候对象还不存在,所以就不存在锁了。
那么,在static方法和非static方法前面加synchronized到底有什么不同呢?
大家都知道,static的方法属于类方法,它属于这个Class(注意:这里的Class不是指Class的某个具体对象),那么static获取到的锁,就是当前调用这个方法的对象所属的类(Class,而不再是由这个Class产生的某个具体对象了)。而非static方法获取到的锁,就是当前调用这个方法的对象的锁了。所以,他们之间不会产生互斥。
看代码:
package com.jack.zhang.chapter9.classlock; public class Test { public static synchronized void staticX() throws InterruptedException { for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("staticX......................."); } } public synchronized void x() throws InterruptedException { for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("x......................."); } } public static void main(String[] args) { final Test test1 = new Test(); Thread thread = new Thread(new Runnable() { public void run() { try { test1.x(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, "a"); Thread thread1 = new Thread(new Runnable() { public void run() { try { Test.staticX(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, "b"); thread1.start(); thread.start(); } }
运行结果是:
staticX....................... x....................... x....................... staticX....................... staticX....................... x....................... x....................... staticX....................... x....................... staticX....................... staticX....................... x....................... x....................... staticX....................... x....................... staticX....................... x....................... staticX....................... x....................... staticX.......................
那当我们想让所有这个类下面的对象都同步的时候,也就是让所有这个类下面的对象共用同一把锁的时候,我们如何办呢?
看代码:
package com.jack.zhang.chapter9.classlock; public class Test { public final static Byte[] locks = new Byte[0]; public static void staticX() throws InterruptedException { synchronized (locks) { for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("staticX......................."); } } } public void x() throws InterruptedException { synchronized (locks) { for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("x......................."); } } } public static void main(String[] args) { final Test test1 = new Test(); final Test test2 = new Test(); Thread thread = new Thread(new Runnable() { public void run() { try { test1.x(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, "a"); Thread thread1 = new Thread(new Runnable() { public void run() { try { Test.staticX(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, "b"); thread1.start(); thread.start(); } }
运行结果:
staticX....................... staticX....................... staticX....................... staticX....................... staticX....................... staticX....................... staticX....................... staticX....................... staticX....................... staticX....................... x....................... x....................... x....................... x....................... x....................... x....................... x....................... x....................... x....................... x.......................