Lock Interface
After Java1.5, and added a contracting Lock Interface (and the associated implementation class) to achieve lock function, which provides synchronization with the synchronized keyword similar function, but in the use of required explicitly acquire and release the lock . Although it lacks the implicit obtain the release of the lock (through synchronized blocks or methods provided) convenience, but it has a variety of synchronized lock acquisition and release key operability, and interruptible lock acquisition timeouts acquire lock do not have the word synchronization characteristics.
Lock very simple to use. As follows:
Lock lock = new ReentrantLock();
lock.lock();
try{
}finally{
lock.unlock();
}
Finally block lock is released, after the object is to ensure that the lock is acquired, it can be finally released.
Do not get locked in the process of writing the try block, as if the acquisition occurred at the time of the lock (custom implementation) anomaly, an exception is thrown at the same time, can lead to the release of the lock for no reason!
The main characteristics of the synchronized keyword Lock interface provided included the following:
characteristic | description |
---|---|
Non-blocking attempt to acquire the lock | The current thread tries to acquire a lock, if the time has not been acquired to other threads, then successfully acquire and hold lock |
Can interrupt access to the lock | And synchronized different threads can acquire a lock to respond to the interrupt, when the acquired lock the thread is interrupted, interrupt exception will be thrown, while the lock is released |
Timeout acquire locks | Acquire a lock before the specified deadline, if the deadline is still unable to acquire a lock, then return |
Lock API interface:
method | description |
---|---|
void lock() | To obtain a lock, the method is called the current thread will acquire the lock when the lock acquisition, returned from the method |
void lockInterruptibly() throws InterruptedException | It may be different from the interruption acquiring the lock, the lock method and that the method can respond to the interrupt, which can interrupt the current thread acquired the lock |
boolean tryLock() | Non-blocking attempt to acquire the lock, return immediately after the method is called, if we can get the returns true, false otherwise |
boolean tryLock(long time, TimeUnit unit) throws InterruptedException | Acquiring the lock timeout, the current thread will return in the following three conditions: 1. The current thread to acquire the lock within the timeout period 2. The current thread is interrupted within the timeout period ends 3. timeout, returns false |
void unlock() | Release the lock |
Condition new Condition() | Gets waiting for a notification component that locks the binding and the current thread, the current thread only get a lock before calling wait () method of the component, and call the current thread releases the lock |
Simulate a simple case to grab votes
package pers.zhang.juc.part1;
import sun.security.krb5.internal.Ticket;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author zhang
* @date 2020/1/17 - 19:54
*/
public class TestLock {
public static void main(String[] args) {
TrainTicket ticket = new TrainTicket();
new Thread(ticket, "1号窗口").start();
new Thread(ticket, "2号窗口").start();
new Thread(ticket, "3号窗口").start();
}
}
class TrainTicket implements Runnable{
private int tick = 100;
private Lock lock = new ReentrantLock();//Lock的实现类
@Override
public void run() {
while(true){
lock.lock(); //上锁
try{
if(tick > 0){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName() + " 完成售票,余票为:" + --tick);
}
}finally{
lock.unlock(); //释放锁
}
}
}
}