Lock lock

Time:2021-9-24

Lock lock (key)

Traditional synchronized

/**
 *Basic ticket example
 *A thread is a separate resource class without any attached operations
 *1. Properties and methods
 */
public class SaleTicketDemo01 {
    public static void main(String[] args) {
        //Concurrency: multiple threads operate on the same resource class and throw the resource class into the thread
        Ticket ticket = new Ticket();
        //@Functional interface, lambda expression after JDK1.8 (parameter) - > {code}
        new Thread(()->{
            for (int i = 1; i < 60; i++) {
                ticket.sale();
            }
        },"A").start();


        new Thread(()->{
            for (int i = 1; i < 60; i++) {
                ticket.sale();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 1; i < 60; i++) {
                ticket.sale();
            }
        },"C").start();
    }

}

//Resource class
class Ticket {
    //Properties and methods
    private int number = 50;

    //How to buy tickets
    //Synchronized: essence, queue, lock
    //Lock object and class
    public synchronized void sale() {
        if (number > 0) {
            System. Out. Println (thread. Currentthread(). Getname() + "sold the first" + number -- + "ticket, remaining: + number);
        }
    }
}

Lock interface

Lock lock

Lock lock

Lock lock

Fair lock: very fair: first come, first served
Unfair lock: very unfair: you can jump the queue (default). Sometimes it will be executed first, and the execution time later is less.
public class SaleTicketDemo02 {
    public static void main(String[] args) {
        Ticket2 ticket2 = new Ticket2();
        new Thread(()->{
            for (int i = 1;i < 40; i++) ticket2.sale();
        },"A").start();

        new Thread(()->{
            for (int i = 1;i < 40; i++) ticket2.sale();
        },"B").start();

        new Thread(()->{
            for (int i = 1;i < 40; i++) ticket2.sale();
        },"C").start();
    }
}


//Resource class
//Lock
class Ticket2 {
    private int number = 30;

    Lock lock = new ReentrantLock();

    public void sale() {

        lock.lock();// Lock

        try {
            //Business code
            if (number > 0) {
                System. Out. Println (thread. Currentthread(). Getname() + "sold the first" + number -- + "ticket, remaining: + number);
            }

        } catch (Exception e) {

        } finally {
            lock.unlock();
        }

    }
}

The difference between synchronized and lock

1. Synchronized is a built-in Java keyword, and lock is a Java class
2. Synchronized cannot determine the status of obtaining the lock. Lock can determine whether the lock has been obtained
3. Synchronized will release the lock automatically. Lock must release the lock manually! If the lock is not released, it will deadlock
4. Synchronized thread 1 (lock acquisition, blocking), thread 2 (wait, wait foolishly); Lock lock doesn't have to wait.
5. Synchronized reentrant lock (reentrant means that a thread has obtained a lock and can obtain the lock again without deadlock). It is non interruptible and unfair. Lock, reentrant lock, can judge whether the lock is unfair (it can be set by itself).
6. Synchronized is suitable for a small number of code synchronization problems, and lock is suitable for locking a large number of synchronization codes!

What is a lock and how to judge who it is

This work adoptsCC agreement, reprint must indicate the author and the link to this article