java多线程-2(八锁问题)

多线程8锁问题

问题:

  1. 两个线程之间睡眠1秒,先打印哪个?
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Test1 test1 = new Test1();
        new Thread(()->{ test1.one(); },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{ test1.two(); },"b").start();
    }
}
class Test1{
    synchronized void one(){ System.out.println("one"); }
    synchronized void two(){ System.out.println("two"); }
}

结果

one

two

  1. 在方法one睡眠5秒,先打印哪个?
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Test2 test2 = new Test2();
        new Thread(()->{
            try {
                test2.one();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            test2.two(); },"b").start();
    }
} 
class Test2{
    synchronized void one() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("one");
    }
    synchronized void two(){ System.out.println("two"); }
}

结果
one

two

  1. 新增一个普通的hello方法,先打印哪个
public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        Test3 test3 = new Test3();
        new Thread(()->{
            try {
                test3.one();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            test3.hello();
        },"b").start();
    }
}
class Test3{
    synchronized void one() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("one");
    }
    void hello(){
        System.out.println("hello");
    }
}

结果
hello

one

  1. 两个不同的对象,先打印哪个?
public class Demo4 {
    public static void main(String[] args) throws InterruptedException {
        Test4 test4 = new Test4();
        Test4 test5_ = new Test4();
        new Thread(()->{
            try {
                test4.one();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            test4_.two();
        },"b").start();
    }
}
class Test4{
    synchronized void one() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("one");
    }
    synchronized void two(){
        System.out.println("two");
    }
}

结果
two

one

  1. 一个普通同步方法,一个静态同步方法,一个对象,先打印哪个?
public class Demo5{
    public static void main(String[] args) throws InterruptedException {
        Test5 test5 = new Test5();
        new Thread(()->{
            try {
                test5.one();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            test5.two();
        },"b").start();
    }
}
class Test5{
    synchronized void one() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("one");
    }
    static synchronized void two(){
        System.out.println("two");
    }
}

结果
two

one

  1. 两个静态同步方法,一个对象,先打印哪个?
public class Demo6{
    public static void main(String[] args) throws InterruptedException {
        Test6 test6 = new Test6();
        new Thread(()->{
            try {
                test6.one();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            test6.two();
        },"b").start();
    }
}
class Test6{
    static synchronized void one() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("one");
    }
    static synchronized void two(){
        System.out.println("two");
    }
}

结果
one

two

  1. 两个静态同步问题,两个对象,先打印哪个?
public class Demo7 {
    public static void main(String[] args) throws InterruptedException {
        Test7 test7 = new Test7();
        Test7 test7_ = new Test7();
        new Thread(()->{
            try {
                test7.one();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            test7_.two();
        },"b").start();
    }
}
class Test7{
    static synchronized void one() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("one");
    }
    static synchronized void two(){
        System.out.println("two");
    }
}

结果
one

two

  1. 一个普通同步方法,一个静态同步方法,两个对象,先打印哪个?
public class Demo8 {
    public static void main(String[] args) throws InterruptedException {
        Test8 test8 = new Test8();
        Test8 test8_ = new Test8();
        new Thread(()->{
            try {
                test8one();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            test8_.two();
        },"b").start();
    }
}
class Test8{
    static synchronized void one() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("one");
    }
    synchronized void two(){
        System.out.println("two");
    }
}

结果
two

one

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值