JUC - multi thread lock problem:

Multi thread lock problem:

  1. 8. Explanation of terms under lock phenomenon:

    • Sequential execution: call first, execute first

    • Random execution: there is no rule, which is related to the computer hardware resources. Which thread gets the resources first, it will execute first, and each thread will not interfere with each other.

  2. Multiple threads use the same lock - sequential execution multiple threads use the same object, multiple threads use a lock, the first call is the first execution!

    public class MultiThreadUseOneLock01 {
        public static void main(String[] args){
    
            Mobile mobile = new Mobile();
            // Two threads use the same object. Two threads are a lock! Call first, execute first!
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // Mobile phone, SMS, email
    class Mobile {
        // The method modified by synchronized and the object of lock are the caller of the method
        public synchronized void sendEmail() {
            System.out.println("sendEmail");
        }
    
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
  3. Multiple threads use the same lock, and one thread has blocking sequential execution in it. Multiple threads use the same object. Multiple threads use one lock, and the one calling first executes, even if blocking is set in a method

    public class MultiThreadUseOneLock02 {
        public static void main(String[] args){
    
            Mobile2 mobile = new Mobile2();
            // Two threads use the same object. Two threads are a lock! Call first, execute first!
            new Thread(()->mobile.sendEmail(),"A").start();
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // Mobile phone, SMS, email
    class Mobile2 {
        // The method modified by synchronized and the object of lock are the caller of the method
        public synchronized void sendEmail() {
    
            //Multiple threads use a lock, and an interference is set here
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
    
  4. Multiple threads are locked or unlocked - multiple threads are executed randomly, some threads are locked, some threads are unlocked, there is no competition between them for the same lock, and the execution sequence is random.

    public class MultiThreadHaveLockAndNot03 {
        public static void main(String[] args){
    
            Mobile3 mobile = new Mobile3();
            // Two threads use the same object. Two threads are a lock! Call first, execute first!
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
    
            new Thread(()->mobile.getWeixinMs(),"C").start();
        }
    }
    
    // Mobile phone, text, email
    class Mobile3 {
        // The method modified by synchronized and the object of lock are the caller of the method
        public synchronized void sendEmail() {
    
            //Multiple threads use a lock, and an interference is set here
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    
        //Receive wechat, no lock
        public void getWeixinMs() {
            System.out.println("getWeixinMs");
        }
    }
    
    
  5. Multiple threads use multiple locks to randomly execute methods modified by synchronized. The object of a lock is the caller of the method. Different callers do not use the same lock and have no relationship with each other

    public class MultiThreadUseMultiLock04 {
        public static void main(String[] args){
    
            // Two objects, do not interfere with each other
            Mobile4 mobile1 = new Mobile4();
            Mobile4 mobile2 = new Mobile4();
    
            new Thread(()->mobile1.sendEmail(),"A").start();
    
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile2.sendMS(),"B").start();
        }
    }
    
    // Mobile phone, SMS, email
    class Mobile4 {
        /**
         *  @description:
         *  Method modified by synchronized, the object of lock is the caller of method;
         *  The callers are different. They do not use the same lock and have no relationship with each other.
         */
        public synchronized void sendEmail() {
    
            //Set up an interference here
            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: multiple threads use an object to sequentially execute methods decorated by synchronized and static at the same time. The object of the lock is the class object of the class, which is the only lock. Sequential execution between threads

    public class MultiThreadUseOneObjectOneClassLock05 {
        public static void main(String[] args){
    
    
            Mobile5 mobile = new Mobile5();
    
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // Mobile phone, SMS, email
    class Mobile5 {
    
    
    
        public synchronized static void sendEmail() {
    
            //Set up an interference here
            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 use multiple objects to sequentially execute methods decorated by synchronized and static. The object of the lock is the class object of the class and the only lock. Class lock is unique, so multiple objects use the same class lock

    public class MultiThreadUseMultiObjectOneClassLock06 {
        public static void main(String[] args){
    
    
            Mobile6 mobile1 = new Mobile6();
            Mobile6 mobile2 = new Mobile6();
    
            new Thread(()->mobile1.sendEmail(),"A").start();
    
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile2.sendMS(),"B").start();
        }
    }
    
    // Mobile phone, SMS, email
    class Mobile6 {
        public synchronized static void sendEmail() {
    
            //Set up an interference here
            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 lock and object lock: multiple threads use an object to randomly execute methods decorated by synchronized and static. The object of the lock is the class object of the class! The only one lock; only the method modified by synchronized is a common lock (such as an object lock), not a class lock, so the execution order between processes does not interfere with each other

    public class MultiThreadUseOneObjectClassLockAndObjectLock07 {
        public static void main(String[] args){
    
    
            Mobile7 mobile = new Mobile7();
    
            new Thread(()->mobile.sendEmail(),"A").start();
    
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile.sendMS(),"B").start();
        }
    }
    
    // Mobile phone, SMS, email
    class Mobile7 {
     
        public synchronized static void sendEmail() {
    
            //Set up an interference here
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        /**
         *  @description:
         *  General synchronization lock: object lock
         */
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
    
  9. Class lock and object lock: multiple threads use multiple objects to randomly execute methods modified by synchronized and static. The object of the lock is the class object of the class! The only one lock; only the method modified by synchronized is a common lock (such as an object lock), not a class lock, so the execution order between processes does not interfere with each other

    public class MultiThreadUseMultiObjectClassLockAndObjectLock08 {
        public static void main(String[] args){
    
            Mobile8 mobile1 = new Mobile8();
            Mobile8 mobile2 = new Mobile8();
            new Thread(()->mobile1.sendEmail(),"A").start();
    
            // interfere
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->mobile2.sendMS(),"B").start();
        }
    }
    
    // Mobile phone, SMS, email
    class Mobile8 {
       
        public synchronized static void sendEmail() {
    
            //Set up an interference here
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("sendEmail");
        }
    
        /**
         *  @description:
         *  General synchronization lock: object lock
         */
        public synchronized void sendMS() {
            System.out.println("sendMS");
        }
    }
    
Published 108 original articles, won praise 8, visited 5144
Private letter follow

Tags: Mobile

Posted on Sun, 08 Mar 2020 00:05:10 -0800 by hd_webdev