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 数据量少 count(1)查询慢_很高兴!终于踩到了慢查询的坑
    C#中的委托(一)
    The hierarchy of the type is inconsistent
    errors exist in required project(s) xxx proceed with launch?
    oracle数据库常用操作语句 、创建视图
    hibernate.hbm.xml必须必须配置主键
    PWC6199:Generated servlet error:Only a type can be imported. org.apache.jasper.tagplugins.jstl.core.ForEach resolves to a package
    org.apache.jasper.JasperException: /WEB-INFO/jsp/product/edit.jsp(168,45)
    unique constraint(PD.HSI_RIGHT) violated
    svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“
  • 原文地址:https://www.cnblogs.com/xlwu/p/13568182.html
Copyright © 2011-2022 走看看