Java中synchronized 修饰在 static方法和 非static方法的区别

Java中synchronized是用来表示同步的,synchronized可以用来修饰一个方法(static方法和非static方法),也可以用来修饰一段代码块;
看代码:

    public synchronized void x() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println("====x======");
        }
    }

    public static synchronized void staticX() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println("=======staticX=====");
        }
    }

static的方法属于类方法,它属于这个Class(注意:这里的Class不是指Class的某个具体对象),那么static获取到的锁,是属于类的锁。而非static方法获取到的锁,是属于当前对象的锁。所以,他们之间不会产生互斥。

main方法:

    public static void main(String[] args) {
        final Test1 test1 = new Test1();
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    test1.x();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "a");

        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Test1.staticX();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "b");
        thread1.start();
        thread2.start();
    }

运行结果:

====x======
=======staticX=====
=======staticX=====
====x======
=======staticX=====
====x======
====x======
=======staticX=====
====x======
=======staticX=====

那当我们想让所有这个类下面的方法都同步的时候,也就是让所有这个类下面的静态方法和非静态方法共用同一把锁的时候,我们如何办呢?

代码如下:

package test;

public class Test2 {
    //创建一个对象  
    final static Test2 ce = new Test2();

    public static void staticX() throws Exception {
        /**
         * 此处的synchronized 和下面的 x()中的synchronized它们调用的都是同一对象‘ce’,
         * 那么此时Test2中的两个方法(所有的方法),公用的都是同一把锁 
         */
        synchronized (ce) {
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println("=======staticX=====");
            }
        }
    }

    public void x() throws Exception {
        synchronized (ce) {
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println("=======x=====");
            }
        }
    }

    public static void main(String[] args) {
        final Test2 test1 = new Test2();
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    test1.x();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "a");

        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Test2.staticX();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "b");
        thread1.start();
        thread2.start();
    }

}

运行结果:

=======x=====
=======x=====
=======x=====
=======x=====
=======x=====
=======staticX=====
=======staticX=====
=======staticX=====
=======staticX=====
=======staticX=====

你可能感兴趣的:(java)