lock和synchronized

A thread has five states:
New: Before calling start,
Ready: After calling start. Before running,
Start: The thread is set as the current thread and starts to execute the run method,
Blocking: the thread is suspended,
death: thread ended

1. The difference between lock and synchronized
(1) synchronize is a keyword of java, and lock is an interface.
(2) Both synchronize and lock are used as locks. Synchronize can release the lock when the thread synchronize method is completed or blocked, while lock needs to manually release the lock in finally, otherwise it may cause deadlock.
(3) The lock can be read by multiple threads at the same time. If one thread occupies the read lock, other threads need to apply for write operations and need to wait for the first thread to release the lock.
If one thread is performing a write operation, the second thread needs to wait for the first thread to release the lock if it wants to perform a read and write operation.
(4) The lock can be read to know whether the thread successfully acquired the lock. synchronize does not work.
(5) lock can make the waiting thread respond to close, synchronize does not work.


2.Lock is easy to use
Lock is an interface with five methods in it. ReentrantLock is the only implementation class of the lock interface
      
    void lock();
    void lockInterruptibly() throws InterruptedException;
    boolean tryLock();
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
    void unlock();

lock() is used to acquire the lock, unlock the user to release the lock
tryLock() is used to check whether the thread successfully acquired the lock, if successful, it returns true, otherwise it returns false
tryLock( long time, TimeUnit unit) is similar to tryLock(), but a time parameter can be added, and if it does not return true within the specified time, it will return false  
lockInterruptibly() is the method of acquiring locks in the lock class. If thread 1 acquires the lock and thread 2 applies for the lock while waiting, you can use the thread2.interrupt() method to make thread 2 respond to shutdown
//Note that if you need to properly interrupt the thread waiting for the lock, you must put the acquisition of the lock outside the try catch, and then throw the InterruptedException

How to use the lock interface to perform multi-threaded read operations at the same time?
ReadWriteLock is also an interface in which two methods are defined.
    Lock readLock();
     Lock writeLock();
Acquire a read lock and acquire a write lock. That is, the read and write operations of the file are separated, so that multi-threaded read operations can be performed . ReentrantReadWriteLock implements the ReadWriteLock interface. (call to acquire the read lock method rwl.readLock().lock(); )



3. Related concepts of locks
1. Reentrant lock
Both synchronize and ReentrantLock() are reentrant locks. Reentrancy can be said to be the allocation mechanism of locks, which is based on thread allocation instead of calling method allocation. For example, if there are two methods in the same object, the synchronize lock is added. When method 1 acquires the lock, method 2 is called inside method 1. If synchronize does not have reentrancy, then when method 1 calls method 2 , Method 2 needs to apply for the lock again, but because method 1 has already obtained the lock, method 2 can never apply for the lock.
2. Interruptible lock
synchronize is not an interruptible lock, lock is an interruptible lock (using the lockInterruptibly() method ).
3. Fair lock
Fair locks allocate locks in the order in which threads request locks. When a thread releases a lock, the thread that has waited the longest obtains the lock first.
synchronize is an unfair lock. So there may be a thread that can never acquire the lock.
lock is an unfair lock by default, but can be configured as a fair lock when instantiated: ReentrantLock lock =  new ReentrantLock( true );  
4. Read-write lock

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326945647&siteId=291194637