zoukankan      html  css  js  c++  java
  • 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");
        }
    }
  • 相关阅读:
    Oracle Instant Client 配置
    释放至强平台 AI 加速潜能 汇医慧影打造全周期 AI 医学影像解决方案
    Analytics Zoo Cluster Serving自动扩展分布式推理
    基于时序数据,推动智能运维发展
    助力用户选择更优模型和架构,推动 AI机器视觉落地智能制造
    英特尔与 Facebook 合作采用第三代英特尔® 至强® 可扩展处理器和支持 BFloat16 加速的英特尔® 深度学习加速技术,提高 PyTorch 性能
    如何无缝地将人工智能扩展到分布式大数据
    Burger King使用RayOnSpark进行基于实时情景特征的快餐食品推荐
    如何往Spark社区做贡献,贡献代码
    开源:从社区到商业化
  • 原文地址:https://www.cnblogs.com/xlwu/p/13568182.html
Copyright © 2011-2022 走看看