This is the 10th day of my participation in the August More Text Challenge

In Java lock

  • The lock is a kind of utilizationsynchronizedCreate a synchronization mechanism similar to direct usesynchronizedKeyword, but more complex than that.java.util.concurrent.locksContains multiple lock implementations, so you don’t need to customize your own locks in most cases.

A simple lock

private int count = 0; public int inc(){ synchronized(this){ return ++count; }}Copy the code
  • Only one thread at a time can execute the actual code block, implementing the thread lock effect.
private Lock lock = new Lock();
  private int count = 0;

  public int inc(){
    lock.lock();
    int newCount = ++count;
    lock.unlock();
    return newCount;
  }
Copy the code
public class Lock{ private boolean isLocked = false; public synchronized void lock() throws InterruptedException{ while(isLocked){ wait(); } isLocked = true; } public synchronized void unlock(){ isLocked = false; notify(); }}Copy the code
  • As above, the synchronized keyword is replaced by a custom lock to achieve the locking effect of the thread. In the custom lock class, the synchronized keyword is used to lock the locking method and the unlocking method, so only one thread executes the locking and unlocking code block at the same time.
  • In a while loop, you can think of it as a spin lock. When the lock semaphore is true, it is locked, and the block of code in the lock goes into spin wait. Until some point when another thread executes the unlock code to reset the variable to false, the lock thread continues to execute and changes the state of the lock semaphore. Wait () and notify() lock and release locks between unlock codes. Synchronized methods modify methods, which are equivalent to locking instances. Therefore, wait() and notify() are required for wake up communication.

Reentrant lock

  • The JavasynchronizedIt’s a reentrant lock. A reentrant lock means that when a Java thread executes on a synchronized code block, that object is locked and that thread can access other synchronized code blocks under the same monitor object.
synchronized void start(){
    this.end();
}

synchronized void end(){
    
}
Copy the code
  • The objects that this code locks are, in theory, this, which is the instance object. When a thread forstart()Method is accessible because both methods hold the same lock instance objectend()And this is reentrant.

Fair lock

  • The JavasynchronizedThere is no guarantee of the order of execution of the threads that enter the code block, so it is possible for threads to wait for execution but never compete with other threads, creating the problem of thread starvation. So in theory locks should be fair to avoid this problem.
  • In addition to the lock fairness approach mentioned in the previous article, note that unlocking is required and guaranteed to be performed in finally. This is also to prevent the lock from being released properly if an exception is thrown in the executing code, rather than holding the lock for a long time and causing a deadlock.

Afterword.

  • How many events through the ages? Leisurely. The Yangtze River is still rolling.