Talk about your understanding of the lock? Deadlock?

Talk about your understanding of the lock?

Lock refers to concurrent programming, in order to guarantee data consistency when multiple threads at the same time operating a set of resources , we need to line up a multi-threaded operating a resource, and this process is to release the lock and resource locking process as if to go to public toilets, you must line up one by one to use, and the need to lock the door and open the door while using the same.

Talk about your understanding of the deadlock?

Deadlock refers to two or more threads in the implementation process, due to the competition for resources or A blocking phenomenon caused due communicate with each other , without external force, they will not be able to promote it. At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock.

Manually simulate a deadlock?

public  class DeadLockExample {
     public  static  void main (String [] args) {
         // Deadlock 
        DEADLOCK (); 
    } 

    public  static  void () {DEADLOCK 

        Object lock1 = new new Object (); 
        Object lock2 = new new Object (); 

        // thread lock1 have tried to get a lock2 
        new new the Thread (() -> {
             the synchronized (lock1) { 
                System.out.println ( "get lock1 success" );
                 the try {
                    TimeUnit.SECONDS.sleep (. 3 ); 
                } the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
                // attempt to acquire the lock lock2 
                the synchronized (lock2) {
                     // get the current thread name 
                    System.out.println (Thread.currentThread () getName () ). ; 
                } 
            } 
        .}) Start (); 

        // thread has two lock2 trying to get lock1 
        new new the thread (() -> {
             the synchronized (lock2) { 
                System.out.println ( "get lock2 success" );
                 {the try 
                    TimeUnit.SECONDS.sleep ( . 3 ); 
                } the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
                // attempt to acquire the lock lock1 
                the synchronized (lock1) {
                     // get the current thread name 
                    System.out.println (Thread.currentThread () .getName ()); 
                } 
            } 
        .}) Start (); 


    } 

}

operation result:

Common interview questions:

(1) Talk about your understanding of the lock?

(2) What is a deadlock? Manually simulate a deadlock?

 Reference / good text:

Baidu Encyclopedia - Deadlock

https://baike.baidu.com/item/%E6%AD%BB%E9%94%81/2196938?fr=aladdin

Retractor course - Java source code and interview Zhenti

https://kaiwu.lagou.com/course/courseInfo.htm?courseId=59#/detail/pc?id=1766

 

Guess you like

Origin www.cnblogs.com/liaowenhui/p/12610562.html