那么,在static方法和非static方法前面加synchronized到底有什么不同呢?
大家都知道,static的方法属于类方法,它属于这个Class(注意:这里的Class不是指Class的某个具体对
象),那么static获取到的锁,就是当前调用这个方法的对象所属的类(Class,而不再是由这个Class产生
的某个具体对象了)。而非static方法获取到的锁,就是当前调用这个方法的对象的锁了。所以他们之间不
会产生互斥。
package com.jack.zhang.chapter9.classlock; /** * * @author EX-LIUQI001 * */ 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; /** * * @author EX-LIUQI001 * */ public class Test2 { 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 Test2 test1 = new Test2(); 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 { Test2.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.......................