synchronized 与 static synchronized 的区别实例demo

synchronizedstatic synchronized 的区别:
synchronized :锁住的只能锁住一个对象就是当前对象
 static synchronized :锁住的是类,所有对象都会锁住

下面demo实例展示。

1. synchronized的demo

synchronized 锁住的是对象,只锁当前实例。

代码:Demo类和Dog类

Dog类

package com.fanjia.demo_fanjia.test;

/**
 * @author fanjia
 * @description TODO
 * @date 2021/7/29 13:17
 */
public class Dog {
    
    

    //第一次没有加static  则 1和2 会同时打印
    //因为synchronized锁住的是对象,所以这里的sleep不会影响2的打印
    public  synchronized void eat(int name) throws InterruptedException {
    
    
        System.out.println("you are dog"+name);
        System.out.println();
        Thread.sleep(10000);
    }
}

Demo类

new 两个对象,则会同时打印说明  synchronized 锁不住不同对象的实例
package com.fanjia.demo_fanjia.test;

import jdk.nashorn.internal.objects.annotations.Where;

/**
 * @author fanjia
 * @description TODO
 * @date 2021/7/29 13:18
 */
public class Demo {
    
    

    public static void main(String[] args) {
    
    
        Dog dog1 = new Dog();
        Dog dog2 = new Dog();

        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                while(true){
    
    
                    try {
    
    
                        dog1.eat(1);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        }).start();


        new Thread(new Runnable() {
    
    
            @Override
            public void run()  {
    
    
                while(true){
    
    
                    try {
    
    
                        dog2.eat(2);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

}

打印结果

you are dog1

you are dog2

==============================================

2.static synchronized

static synchronized 锁住的是  类,全部对象都会被锁住

demo类和Dog类

Dog类

package com.fanjia.demo_fanjia.test;

/**
 * @author fanjia
 * @description TODO
 * @date 2021/7/29 13:17
 */
public class Dog {
    
    

    //第二次加上static  则 1会先打印, 间隔 10秒, 再打印2
    //因为static synchronized锁住的是类,所有对象,所以这里的sleep会影响2的打印
    public static synchronized void eat(int name) throws InterruptedException {
    
    
        System.out.println("you are dog"+name);
        System.out.println();
        Thread.sleep(10000);
    }
}

Demo类

package com.fanjia.demo_fanjia.test;

import jdk.nashorn.internal.objects.annotations.Where;

/**
 * @author fanjia
 * @description TODO
 * @date 2021/7/29 13:18
 */
public class Demo {
    
    

    public static void main(String[] args) {
    
    
        Dog dog1 = new Dog();
        Dog dog2 = new Dog();

        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                while(true){
    
    
                    try {
    
    
                        dog1.eat(1);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        }).start();


        new Thread(new Runnable() {
    
    
            @Override
            public void run()  {
    
    
                while(true){
    
    
                    try {
    
    
                        dog2.eat(2);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

}

打印结果

2还没打印出来,会间隔10秒

you are dog1

Guess you like

Origin blog.csdn.net/qq_43472248/article/details/119209339