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");
        }
    }
  • 相关阅读:
    MongoDb学习(五)--Gridfs--上传下载
    MongoDb学习(四)--Repository----语法关键字
    MongoDb学习(四)--Repository
    MongoDB插入数据,除去Class key
    MongoDb学习三(spring-data-mongodb)
    MongoDb二
    java连接mongodb数据库
    MongoDb笔记
    java字符统计+字符串压缩
    安卓蓝牙操作+蓝牙工具类
  • 原文地址:https://www.cnblogs.com/xlwu/p/13568182.html
Copyright © 2011-2022 走看看