Multithreaded programming (7) Lock Lock

Lock

1. Two lock ports

  • Lock: Supports reentrant, fairness and other lock rules, implementation classes: ReentrantLock, ReadLock, WriteLock
  • ReadWriteLock: The interface defines a lock shared by readers and exclusive locks for writers. Implementation class: ReentrantReadWriteLock

2. Reentrant lock

  • Non-reentrant lock: that is, the thread will block when requesting the lock he already has
  • Reentrant lock: that is, the thread can enter the synchronization code block of the lock that it already has

Test code

public class ReentrantLockTest {
    
    

    static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
    
    
        for (int i = 0; i < 10; i++) {
    
    
            lock.lock();
            System.out.println("加锁次数" + i);
        }

        for (int i = 0; i < 10; i++) {
    
    
            try {
    
    
                System.out.println("解锁次数" + i);
            } finally {
    
    
                lock.unlock();
            }
        }
    }
}

Test Results

加锁次数0
加锁次数1
加锁次数2
加锁次数3
加锁次数4
加锁次数5
加锁次数6
加锁次数7
加锁次数8
加锁次数9
解锁次数0
解锁次数1
解锁次数2
解锁次数3
解锁次数4
解锁次数5
解锁次数6
解锁次数7
解锁次数8
解锁次数9

3. Read-write lock

  • Both can read at the same time, but cannot write while reading,
  • Can not write at the same time, cannot read while writing

Test code

public class ReadWriteLockTest {
    
    

    //要访问的对象
    private Map<String, String> map = new HashMap<>();

    ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    //读锁
    ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    //写锁
    ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();


    public String get(String key) {
    
    

        try {
    
    
            readLock.lock();
            System.out.println(Thread.currentThread().getName() + "线程读锁已加锁");
            Thread.sleep(3000);
            return map.get(key);
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return null;
        } finally {
    
    
            readLock.unlock();
            System.out.println(Thread.currentThread().getName() + "线程读锁已解锁");
        }
    }


    public void put(String key, String value) {
    
    

        try {
    
    
            readLock.lock();
            System.out.println(Thread.currentThread().getName() + "线程写锁已加锁");
            Thread.sleep(3000);
            map.put(key, value);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            readLock.unlock();
            System.out.println(Thread.currentThread().getName() + "线程写锁已解锁");
        }
    }


    public static void main(String[] args) {
    
    
        ReadWriteLockTest readWriteLockTest = new ReadWriteLockTest();
        readWriteLockTest.put("key1", "value1");

        for (int i = 0; i < 5; i++) {
    
    

            new Thread(new Runnable() {
    
    
                @Override
                public void run() {
    
    
                    readWriteLockTest.get("key1");
                }
            }).start();

        }
    }
}

Test Results

main线程写锁已加锁
main线程写锁已解锁
Thread-0线程读锁已加锁
Thread-1线程读锁已加锁
Thread-2线程读锁已加锁
Thread-3线程读锁已加锁
Thread-4线程读锁已加锁
Thread-0线程读锁已解锁
Thread-3线程读锁已解锁
Thread-4线程读锁已解锁
Thread-1线程读锁已解锁
Thread-2线程读锁已解锁

Conclusion Read locks are executed after write locks

Guess you like

Origin blog.csdn.net/jinian2016/article/details/109703410