通过8种加锁情况来弄懂加锁对于线程执行顺序的影响

  • 1个资源类对象,2个线程,2个同步方法,第二个线程等待1s后开启。
//资源类
public class Example {
    //2个同步方法
    public synchronized void method1(){
        System.out.println("线程1正在执行...");
    }

    public synchronized void method2(){
        System.out.println("线程2正在执行...");
    }
}

//开启线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //1个资源类对象
        Example example=new Example();

        //创建并开启2个线程
        new Thread(()->{
            example.method1();
        }).start();

        //第二个线程延时1s创建并开启
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            example.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

        线程1和线程2使用的是同一个对象调用的同步方法,所以这两个线程需要竞争同一个锁。而线程1先运行,线程2延迟1s后才被创建,所以这1s期间只有线程1在竞争锁,就必然是线程1先获得锁,线程2只能等待线程1执行完后才能获得锁,然后才可以执行。这里面锁是很重要的,如果没有锁线程2完全可以在线程1执行期间抢夺CPU的占用权,并且如果线程1和线程2都需要较长的运行时间,那么谁最后执行完就不一定了。

  • 1个资源类对象,2个线程,2个同步方法,第一个线程开启后休眠3s,第二个线程等待1s后开启。
//资源类
public class Example {
    //2个同步方法
    public synchronized void method1() throws InterruptedException {
        //开启后先休眠3s
        TimeUnit.SECONDS.sleep(3);
        System.out.println("线程1正在执行...");
    }

    public synchronized void method2(){
        System.out.println("线程2正在执行...");
    }
}

//开启两个线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //1个资源类对象
        Example example=new Example();

        //创建并开启2个线程
        new Thread(()->{
            try {
                example.method1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        //第二个线程延时1s创建并开启
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            example.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

        对比上面的情况,线程1在开启后先休眠3s,这对于执行结果是没有影响的。因为线程在调用sleep后进入计时等待状态,在此状态下如果已经获取了锁则不会释放,也就是抱着锁睡觉;区别于wait,调用wait后线程进入等待状态,在此期间如果之前已经获得了锁则会释放。 所以虽然休眠了3s,但线程1仍旧持有锁,线程2只能等待。

  • 1个资源类对象,2个线程,1个同步方法+1个普通方法,第一个线程开启后休眠3s。
//资源类
public class Example {
    //1个同步方法
    public synchronized void method1() throws InterruptedException {
        //开启后休眠3s            
        TimeUnit.SECONDS.sleep(3);
        System.out.println("线程1正在执行...");
    }

    //1个普通方法
    public void method2(){
        System.out.println("线程2正在执行...");
    }
}

//创建两个线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //1个资源类对象
        Example example=new Example();

        //创建并开启2个线程
        new Thread(()->{
            try {
                example.method1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        new Thread(()->{
            example.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

        虽然线程1先获得了锁,并且sleep休眠期间也持有锁,但线程2调用的方法是普通方法,不需要获得锁即可执行。加锁只能防止其他需要获得同一把锁的线程,并不能防止不需要锁或者需要其他锁的线程,所以这种情况下锁就没有用了,线程1先获得了CPU占用权,随后休眠期间CPU被线程2抢走并执行,3s足以让线程2执行完毕,休眠结束后线程1再执行。

  • 2个资源类对象分别开启1个线程,2个同步方法,第一个线程开启后休眠3s,第二个线程等待1s后开启。
//资源类
public class Example {
    //2个同步方法
    public synchronized void method1() throws InterruptedException {
        //先休眠3s
        TimeUnit.SECONDS.sleep(3);
        System.out.println("线程1正在执行...");
    }

    public synchronized void method2(){
        System.out.println("线程2正在执行...");
    }
}

//创建两个线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //2个资源类对象
        Example example1=new Example();
        Example example2=new Example();
        
        //2个资源类对象分别创建并开启1个线程
        new Thread(()->{
            try {
                example1.method1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        //第二个线程等待1s后创建
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            example2.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

        非静态同步方法使用的锁对象是this,也就是方法的调用者,而本案例中使用的是两个不同的资源类对象分别创建的线程,所以两个同步方法使用的锁对象是不一样的,第一个线程使用的锁对象是example1,第二个使用的是example2;上面的案例中谈到加锁并不能防止不需要锁或者需要其他锁的线程,所以虽然线程1先抢到了锁,但是在休眠的3s内CPU是线程2的,并且线程2需要获取的是另一把锁,这就使得线程2会比线程1先执行完。

  • 1个资源类对象,2个线程,2个静态同步方法,第一个线程开启后休眠3s,第二个线程等待1s后开启。
//资源类
public class Example {
    //2个静态同步方法
    public static synchronized void method1() throws InterruptedException {
        //先休眠3s
        TimeUnit.SECONDS.sleep(3);
        System.out.println("线程1正在执行...");
    }

    public static synchronized void method2(){
        System.out.println("线程2正在执行...");
    }
}

//创建两个线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //1个资源类对象
        Example example=new Example();

        //创建并开启2个线程
        new Thread(()->{
            try {
                example.method1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        //第二个线程等待1s后创建
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            example.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

        静态同步方法的锁对象使用的是类的Class对象,而这个Class对象一个类只有一个,所以同一个类下所有静态同步方法的锁对象是同一个,即这个类的Class对象。那为什么会选择Class对象作为锁呢?这是因为静态方法和Class对象在类加载阶段就已经加载好了,但创建的对象需要等到类加载后执行到创建对象的代码时才会创建,只能选择Class对象作为锁对象。

        既然锁对象是同一个,并且线程1先抢到锁,那必然是线程1先执行,线程2后执行了。

  • 2个资源类对象分别开启1个线程,2个静态同步方法,第一个线程开启后休眠3s,第二个线程等待1s后开启。
//资源类
public class Example {
    //2个静态同步方法
    public static synchronized void method1() throws InterruptedException {
        //先休眠3s
        TimeUnit.SECONDS.sleep(3);
        System.out.println("线程1正在执行...");
    }

    public static synchronized void method2(){
        System.out.println("线程2正在执行...");
    }
}

//创建两个线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //2个资源类对象
        Example example1=new Example();
        Example example2=new Example();

        //两个对象各创建并开启1个线程
        new Thread(()->{
            try {
                example1.method1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        //第二个线程等待1s后创建
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            example2.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

        上面说到同一个类中所有静态的同步方法使用的锁对象都是这个类的Class对象,那么在这个案例中,不同的资源类对象对锁对象没有影响,线程1和线程2需要获取的锁是一样的。由于线程1先获取到锁,所以线程1先执行,线程2后执行。

  • 1个资源类对象,2个线程,1个静态同步方法+1个同步方法,第一个线程开启后休眠3s,第二个线程等待1s后开启。
//资源类
public class Example {
    //1个静态同步方法
    public static synchronized void method1() throws InterruptedException {
        //先休眠3s
        TimeUnit.SECONDS.sleep(3);
        System.out.println("线程1正在执行...");
    }

    //1个同步方法
    public synchronized void method2(){
        System.out.println("线程2正在执行...");
    }
}

//创建两个线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //1个资源类对象
        Example example=new Example();

        //创建并开启2个线程
        new Thread(()->{
            try {
                example.method1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        //第二个线程等待1s后创建
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            example.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

        对于线程1,静态方法的锁对象是Example.class;对于线程2,非静态同步方法的锁对象是方法调用者,即example,所以两个线程需要获取的锁是不同的,那么线程2就会在线程1休眠的时间里先执行完。

  • 2个资源类对象分别开启1个线程,1个静态同步方法+1个同步方法,第一个线程开启后休眠3s,第二个线程等待1s后开启。
//资源类
public class Example {
    //1个静态同步方法
    public static synchronized void method1() throws InterruptedException {
        //先休眠3s
        TimeUnit.SECONDS.sleep(3);
        System.out.println("线程1正在执行...");
    }

    //1个同步方法
    public synchronized void method2(){
        System.out.println("线程2正在执行...");
    }
}

//创建两个线程
public class Main {
    public static void main(String[] args) throws InterruptedException {
        //2个资源类对象
        Example example1=new Example();
        Example example2=new Example();

        //2个对象各创建并开启1个线程
        new Thread(()->{
            try {
                example1.method1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        //第二个线程等待1s后创建
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            example2.method2();
        }).start();
    }
}

        先输出那个语句呢?

        运行结果:

          对于线程1,静态方法的锁对象是Example.class;对于线程2,非静态同步方法的锁对象是方法调用者,即example2,所以两个线程需要获取的锁是不同的,那么线程2就会在线程1休眠的时间里先执行完。

  • 21
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值