JUC - Multi-threaded locking problems:

Multi-threaded locking problems:

Here Insert Picture Description

  1. 8-lock phenomenon Glossary:

    • The order of execution: first call before execution

    • Random execution: there is no law, related to computer hardware resources, which thread to get resources on the first execution, interference between each thread.

  2. Multiple threads using the same lock - execution order multiple threads using the same object, a lock is to use multiple threads, the first call to execute!

    public class MultiThreadUseOneLock01 {
        public static void main(String[] args){
    
            Mobile mobile = new Mobile();
            // 两个线程使用的是同一个对象。两个线程是一把锁!先调用的先执行!
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile {
        // 被 synchronized 修饰的方法、锁的对象是方法的调用者、
        public synchronized void sendEmail() {
            System.out.println("sendEmail");
        }
    
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
  3. Multiple threads using the same lock, where there is also a thread blocking - to execute multiple threads **** order to use the same object, a lock is to use multiple threads, the first implementation of the first call, even in a method It is set to block

    public class MultiThreadUseOneLock02 {
        public static void main(String[] args){
    
            Mobile2 mobile = new Mobile2();
            // 两个线程使用的是同一个对象。两个线程是一把锁!先调用的先执行!
            new Thread(()->mobile.sendEmail(),"A").start();
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile2 {
        // 被 synchronized 修饰的方法、锁的对象是方法的调用者、
        public synchronized void sendEmail() {
    
            //多个线程使用一把锁,这里设置一个干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
    
  4. There are multiple threads and locks unlocked - random execute multiple threads, and some thread lock, and some did not lock the thread, competing for the same lock situation does not exist between the two, has the execution order is random.

    
    public class MultiThreadHaveLockAndNot03 {
        public static void main(String[] args){
    
            Mobile3 mobile = new Mobile3();
            // 两个线程使用的是同一个对象。两个线程是一把锁!先调用的先执行!
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
    
            new Thread(()->mobile.getWeixinMs(),"C").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile3 {
        // 被 synchronized 修饰的方法、锁的对象是方法的调用者、
        public synchronized void sendEmail() {
    
            //多个线程使用一把锁,这里设置一个干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    
        //接收微信,没有锁
        public void getWeixinMs() {
            System.out.println("getWeixinMs");
        }
    }
    
    
  5. Multiple threads using multiple locks - random execution modified synchronized method, the object is to lock the caller of the method; different callers, not using the same lock, there is no relationship to each other between them

    
    public class MultiThreadUseMultiLock04 {
        public static void main(String[] args){
    
            // 两个对象,互不干预
            Mobile4 mobile1 = new Mobile4();
            Mobile4 mobile2 = new Mobile4();
    
            new Thread(()->mobile1.sendEmail(),"A").start();
    
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile2.sendMS(),"B").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile4 {
        /**
         *  @description:
         *  被 synchronized 修饰的方法,锁的对象是方法的调用者;
         *  调用者不同,它们之间用的不是同一个锁,相互之间没有关系。
         */
        public synchronized void sendEmail() {
    
            //这里设置一个干扰
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
  6. Class Lock: a plurality of threads using an object - performing simultaneously modified and synchronized sequential static method, the lock object is a class object class, is the only one lock. Between threads are executed sequentially

    
    public class MultiThreadUseOneObjectOneClassLock05 {
        public static void main(String[] args){
    
    
            Mobile5 mobile = new Mobile5();
    
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile5 {
    
    
    
        public synchronized static void sendEmail() {
    
            //这里设置一个干扰
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        public synchronized static void sendMS() {
            System.out.println("sendMS");
        }
    }
    
    
  7. Class Lock: multiple threads using multiple objects - execution modified and synchronized static method sequence modifications, lock objects are class object class is the only one lock. Class lock is unique, so multiple objects using the same Class also lock

    
    public class MultiThreadUseMultiObjectOneClassLock06 {
        public static void main(String[] args){
    
    
            Mobile6 mobile1 = new Mobile6();
            Mobile6 mobile2 = new Mobile6();
    
            new Thread(()->mobile1.sendEmail(),"A").start();
    
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile2.sendMS(),"B").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile6 {
        public synchronized static void sendEmail() {
    
            //这里设置一个干扰
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        public synchronized static void sendMS() {
            System.out.println("sendMS");
        }
    }
    
  8. Class locks and lock objects: one object using a plurality of threads - randomly modified synchronized and perform static method, the lock object is a class object class! Only the same lock; modified only by synchronized methods, ordinary lock (such as object lock), Class is not locked, so no interference between the execution order process

    public class MultiThreadUseOneObjectClassLockAndObjectLock07 {
        public static void main(String[] args){
    
    
            Mobile7 mobile = new Mobile7();
    
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile7 {
     
        public synchronized static void sendEmail() {
    
            //这里设置一个干扰
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        /**
         *  @description:
         *  普通同步锁:对象锁
         */
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
    
  9. Class and object lock lock: multiple threads using multiple objects - random execution modified and synchronized static method, lock the target object class is class! Only the same lock; modified only by synchronized methods, ordinary lock (such as object lock), Class is not locked, so no interference between the execution order process

    public class MultiThreadUseMultiObjectClassLockAndObjectLock08 {
        public static void main(String[] args){
    
            Mobile8 mobile1 = new Mobile8();
            Mobile8 mobile2 = new Mobile8();
            new Thread(()->mobile1.sendEmail(),"A").start();
    
            // 干扰
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile2.sendMS(),"B").start();
        }
    }
    
    // 手机,发短信,发邮件
    class Mobile8 {
       
        public synchronized static void sendEmail() {
    
            //这里设置一个干扰
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        /**
         *  @description:
         *  普通同步锁:对象锁
         */
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
Published 108 original articles · won praise 8 · views 5208

Guess you like

Origin blog.csdn.net/qq_41922608/article/details/104733337