当前位置:网站首页>Lock lock

Lock lock

2022-04-23 16:57:00 Wooden horse

First look at the official documents :
 Insert picture description here
Implementation classes have reentrant locks 、 Read lock and write lock , Reentrant locks are most commonly used .
Reentrant lock , To refer to Threads In units of , When a thread acquires an object lock , This thread can get the lock on this object again , Other threads are not allowed .

 Insert picture description here
You can see ,ReentrantLock yes lock An implementation class of the interface , It implements reentrant lock, fair lock and unfair lock .
Fair lock : Very fair , first come , first served .
Not fair lock : It's very unfair , You can jump in line .
The default implementation is unfair lock .
 Insert picture description here
Its whole logic is : Lock , stay try Write business code in , stay finally Inside unlock .

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/** * @author DB * @title: Test02 * @projectName Juc * @description: TODO * @date 2022/4/21 10:34 */
public class Test02 {
    
    public static void main(String[] args) {
    
        Ticket2 ticket2 = new Ticket2();

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

    }
}

class Ticket2 {
    
    // attribute 
    private int number = 30;
    Lock lock = new ReentrantLock();

    // Method 
    public synchronized void sale(){
    
        // Lock 
        lock.lock();

        try {
    
            // Write business code 
            if (number > 0){
    
                System.out.println(Thread.currentThread().getName() + " Sold " + (number --) + " ticket , The remaining " + number);
            }
        }catch (Exception e){
    
            e.printStackTrace();
        }finally {
    
            // Unlock 
            lock.unlock();
        }
    }
}

result :
 Insert picture description here

Synchronized and Lock The difference between ?

(1)Synchronized yes Java Built in keywords for ;Lock It's a Java class .

(2)Synchronized Unable to determine the status of the acquired lock ;Lock It can be judged whether the lock is obtained .

(3)Synchronized Will automatically release the lock ;lock Lock must be released manually , If you don't release -> Deadlock .

(4)Synchronized Threads 1( Gets the lock , Blocking ), Threads 2( wait for , Silly wait );lock The lock doesn't have to wait .

(5)Synchronized Reentrant lock , It can't be interrupted , Not fair lock ;Lock lock , Reentrant lock , Can be judged , You can set whether it is fair or unfair .

(6)Synchronized Suitable for locking a small number of code synchronization problems ;Lock Suitable for locking a large number of synchronization code .

版权声明
本文为[Wooden horse]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231653336593.html