synchronized Java中锁的面试题

面试题1:
    doOther方法执行的时候需要等待doSome方法的结束吗? 相同对象单个方法锁
package com.javaSe.exam1;
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
// 不需要 因为doOther方法没有synchronized。
public class exam01 {
    public static void main(String[] args) {
        MyClass mc = new MyClass();
        
        Thread t1 = new MyThread(mc);
        Thread t2 = new MyThread(mc);
        
        t1.setName("t1");
        t2.setName("t2");
        
        t1.start();
        try {
            Thread.sleep(1000);// 这个睡眠的作用是:为了保证t1线程先执行。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.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 end");
    }
    
    public void doOther(){
        System.out.println("doOther begin");
        
        System.out.println("doOther end");
    }
}
面试题2:
    doOther方法执行的时候需要等待doSome方法的结束吗?   相同对象方法锁
package com.javaSe.exam2;
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
// 需要 因为doOther方法有synchronized了。
public class exam01 {
    public static void main(String[] args) {
        MyClass mc = new MyClass();
        
        Thread t1 = new MyThread(mc);
        Thread t2 = new MyThread(mc);
        
        t1.setName("t1");
        t2.setName("t2");
        
        t1.start();
        try {
            Thread.sleep(1000);// 这个睡眠的作用是:为了保证t1线程先执行。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.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 end");
    }
    
    public synchronized void doOther(){
        System.out.println("doOther begin");
        
        System.out.println("doOther end");
    }
}
面试题3:
    doOther方法执行的时候需要等待doSome方法的结束吗? 不同对象方法锁
package com.javaSe.exam3;
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
// 不需要 因为MyClass对象是两个,他们是不同的锁,所以没有关联。线程对象不共享
public class exam01 {
    public static void main(String[] args) {
        MyClass mc1 = new MyClass();
        MyClass mc2 = new MyClass();
        
        Thread t1 = new MyThread(mc1);
        Thread t2 = new MyThread(mc2);
        
        t1.setName("t1");
        t2.setName("t2");
        
        t1.start();
        try {
            Thread.sleep(1000);// 这个睡眠的作用是:为了保证t1线程先执行。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.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 end");
    }
    
    public synchronized void doOther(){
        System.out.println("doOther begin");
        
        System.out.println("doOther end");
    }
}
 
面试题4:
    doOther方法执行的时候需要等待doSome方法的结束吗?   类锁
package com.javaSe.exam4;
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
// 需要 因为静态方法是类锁,不管创建了几个对象,类锁只有一把。
public class exam01 {
    public static void main(String[] args) {
        MyClass mc1 = new MyClass();
        MyClass mc2 = new MyClass();
        
        Thread t1 = new MyThread(mc1);
        Thread t2 = new MyThread(mc2);
        
        t1.setName("t1");
        t2.setName("t2");
        
        t1.start();
        try {
            Thread.sleep(1000);// 这个睡眠的作用是:为了保证t1线程先执行。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.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{
    // synchronized出现在静态方法上,是找类锁。
    public synchronized static void doSome(){
        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } 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");
    }
}
原文地址:https://www.cnblogs.com/xlwu/p/13568182.html