synchronized面试题

1.doOther方法执行需要等待doSome方法执行结束吗?

main:{
        MyClass myClass = new MyClass();
        Thread t1 = new MyThread(myClass);
        Thread t2 = new MyThread(myClass);

        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }

    class MyThread extends Thread{
        private MyClass mc;

        public MyThread(MyClass mc) {
            this.mc = mc;
        }

        @Override
        public void run() {
            if(Thread.currentThread().getName().equals("t1")){
                mc.doSome();
            }else{
                mc.doOther();
            }
        }
    }

    class MyClass{
        public synchronized void doSome(){
            System.out.println("doSome begin ......");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("doSome end ......");
        }
        public void doOther(){
            System.out.println("doOther begin ......");
            System.out.println("doOther end ......");
        }
    }

 答:不需要,doOther没有synchronized修饰,不需要共享对象锁,正常执行。如果doOther方法中加上synchronized,则需要等待doSome方法结束。

2.doOther方法执行需要等待doSome方法执行结束吗?

main:{
        MyClass myClass1 = new MyClass();
        MyClass myClass2 = new MyClass();
        Thread t1 = new MyThread(myClass1);
        Thread t2 = new MyThread(myClass2);

        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }

    class MyThread extends Thread{
        private MyClass mc;

        public MyThread(MyClass mc) {
            this.mc = mc;
        }

        @Override
        public void run() {
            if(Thread.currentThread().getName().equals("t1")){
                mc.doSome();
            }else{
                mc.doOther();
            }
        }
    }

    class MyClass{
        public synchronized void doSome(){
            System.out.println("doSome begin ......");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("doSome end ......");
        }
        public synchronized void doOther(){
            System.out.println("doOther begin ......");
            System.out.println("doOther end ......");
        }
    }

答:不需要,因为MyClass是两个对象,是两个对象锁

3.doOther方法执行需要等待doSome方法执行结束吗?

main:{
        MyClass myClass1 = new MyClass();
        MyClass myClass2 = new MyClass();
        Thread t1 = new MyThread(myClass1);
        Thread t2 = new MyThread(myClass2);

        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }

    class MyThread extends Thread{
        private MyClass mc;

        public MyThread(MyClass mc) {
            this.mc = mc;
        }

        @Override
        public void run() {
            if(Thread.currentThread().getName().equals("t1")){
                mc.doSome();
            }else{
                mc.doOther();
            }
        }
    }

    static class MyClass{
        public synchronized static void doSome(){
            System.out.println("doSome begin ......");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("doSome end ......");
        }
        public synchronized static void doOther(){
            System.out.println("doOther begin ......");
            System.out.println("doOther end ......");
        }
    }

答:需要等,因为synchronized在静态方法中是类锁,不管创建了几个对象,类锁只有一把。

4.死锁

main:{
        Object o1 = new Object();
        Object o2 = new Object();
        Thread t1 = new MyThread1(o1,o2);
        Thread t2 = new MyThread2(o1,o2);

        t1.start();
        t2.start();
    }

    class MyThread1 extends Thread{
        Object o1;
        Object o2;

        public MyThread1(Object o1, Object o2) {
            this.o1 = o1;
            this.o2 = o2;
        }

        @Override
        public void run() {
            synchronized (o1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){

                }
            }
        }
    }

    class MyThread2 extends Thread{
        Object o1;
        Object o2;

        public MyThread2(Object o1, Object o2) {
            this.o1 = o1;
            this.o2 = o2;
        }

        @Override
        public void run() {
            synchronized (o2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){

                }
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值