synchronized面试题

1、doSome方法执行需要等待dother方法执行吗
不需要

package com;

class test {
    public static void main(String[] args) thorows InterruptedExcption {
        //创建对象
        MyClass mc = new MyClass();
        //创建线程对象
        MyThread m = new MyThread(mc);
        MyThread m1 = new MyThread(mc);
        //设置线程名字
        m.setName("t1");
        m1.setName("t2");
        //启动
        m.start();
        //这个睡眠是保证t1线程先执行
        Thread.sleep(1000);
        m1.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;

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

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

}

class MyClass {


    public synchronized void doSome() {

        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }

    public void doOther() {

        System.out.println("doOther begin");

        System.out.println("doOther over");
    }
}

2、doSome方法执行需要等待doOther方法吗
需要。2把类锁

package com;

class test {
    public static void main(String[] args) thorows InterruptedExcption {
        //创建对象
        MyClass mc = new MyClass();
        //创建线程对象
        MyThread m = new MyThread(mc);
        MyThread m1 = new MyThread(mc);
        //设置线程名字
        m.setName("t1");
        m1.setName("t2");
        //启动
        m.start();
        //这个睡眠是保证t1线程先执行
        Thread.sleep(1000);
        m1.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;

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

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

}

class MyClass {


    public synchronized void doSome() {

        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }

    public synchronized  void doOther() {

        System.out.println("doOther begin");

        System.out.println("doOther over");
    }
}

3、doSome方法执行需要doOther方法结束吗
不需要,因为Myclass是2个对象,2把锁

package com;

class test {
    public static void main(String[] args) {
        //创建对象
        MyClass mc = new MyClass();
        MyClass mc1 = new MyClass();
        //创建线程对象
        MyThread m = new MyThread(mc);
        MyThread m1 = new MyThread(mc1);
        //设置线程名字
        m.setName("t1");
        m1.setName("t2");
        //启动
        m.start();
        m1.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;

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

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

}

class MyClass {


    public synchronized void doSome() {

        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }

    public synchronized void doOther() {

        System.out.println("doOther begin");

        System.out.println("doOther over");
    }
}

4、doSome方法执行需要doOther方法结束吗
需要,synchronized 在静态方法上找类锁

package com;

class test {
    public static void main(String[] args) {
        //创建对象
        MyClass mc = new MyClass();
        MyClass mc1 = new MyClass();
        //创建线程对象
        MyThread m = new MyThread(mc);
        MyThread m1 = new MyThread(mc1);
        //设置线程名字
        m.setName("t1");
        m1.setName("t2");
        //启动
        m.start();
        m1.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;

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

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

}

class MyClass {


    public synchronized static void doSome() {

        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }

    public synchronized static void doOther() {

        System.out.println("doOther begin");

        System.out.println("doOther over");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值