Java concurrency lock test and timeout understanding (lock, lockInterruptibly, trylock)

      To prevent blocks of code from being interfered with by concurrent access, the Java language provides locks for this purpose. Assuming a thread calls the method Fmethod (which uses a lock), it will have the right to run until the thread finishes executing the method Fmethod. Assuming that the second thread also calls the method Fmethod, since the second thread cannot acquire the lock, it will be blocked when calling the lock method. It must wait for the first thread to complete the execution of the Fmethod method before it can be activated again. When the first thread releases the lock, the second thread can start running:

  • void lock();

If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired. Sleeps and disables all thread scheduling while waiting for the lock to be acquired.

      In order to ensure that the thread can leave immediately to do other things when it cannot obtain the lock, the tryLock method can be used, which tries to apply for a lock and returns true after successfully obtaining the lock, otherwise it returns false immediately:

if(myLock.tryLock()){
    try{ . . .}    //now the thread owns the lock
    finally{ myLock.unlock();}
}else    //do something else

  • boolean tryLock();

Acquires the lock if it is available and returns immediately with the value true. If the lock is not available then this method will return immediately with the value false. 获取到锁并返回true;获取不到并返回false。

  • boolean tryLock(long time, TimeUnit unit) throws InterruptedException;

If the lock is available this method returns immediately with the value true. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:The lock is acquired by the current thread; or Some other thread interrupts the current thread, and interruption of lock acquisition is supported; or The specified waiting time elapses. 在指定时间内等待获取锁;过程中可被中断。

      In addition, the lockInterruptibly method can also be called. When the thread is interrupted while waiting, an InterruptedException will be thrown, allowing the program to break the deadlock:

  • void lockInterruptibly() throws InterruptedException;

If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:The lock is acquired by the current thread; or Some other thread interrupts the current thread, and interruption of lock acquisition is supported. 在等待获取锁的过程中可被中断。

      The following examples illustrate the use of these four methods in detail: If thread A and thread B use the same lock LOCK, then thread A first acquires the lock LOCK.lock(), and always holds it without releasing it. If B wants to acquire the lock at this time, there are four ways:

LOCK.lock(): This method will always wait, even if B.interrupt() is called, it cannot be interrupted unless thread A calls LOCK.unlock() to release the lock.

LOCK.lockInterruptibly(): This method will wait, but when B.interrupt() is called, it will be interrupted to wait, and an InterruptedException will be thrown, otherwise it will always wait like lock() until thread A releases the lock.

LOCK.tryLock(): There will be no waiting here, no lock will be obtained and false will be returned directly to execute the following logic.

LOCK.tryLock(10, TimeUnit.SECONDS): This place will be waiting for 10 seconds, but when B.interrupt() is called, it will be interrupted and an InterruptedException will be thrown. If thread A releases the lock within 10 seconds, it will acquire the lock and return true; otherwise, after 10 seconds, the lock will not be acquired and return false to execute the following logic.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325898993&siteId=291194637