Home > Java > javaTutorial > How to use lockInterruptibly in java

How to use lockInterruptibly in java

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-05-09 12:01:07
forward
1228 people have browsed it

1. Description

Both threads use lockInterruptibly to acquire the lock. If thread A acquires the lock, thread B can only wait. Calling the interrupt() method on thread B canInterrupt the waiting process of thread B.

2. Note on usage

Since an exception is thrown in the declaration of lockInterruptibly(), lock.lockInterruptibly() must be placed in the try blockIn or outside the method calling lockInterruptibly() declare that InterruptedException is thrown.

3. Example

public  void  method()  throws  InterruptedException {
     lock.lockInterruptibly();
     try  {  
      //.....
     }
     finally  {
         lock.unlock();
     }  
}
Copy after login

When a lock is acquired through the lockInterruptibly() method, if it cannot be acquired, it is possible to respond to the interrupt only by waiting. of.

What collection classes are there in Java?

Collections in Java are mainly divided into four categories:

1. List: ordered, repeatable;

2. Queue: ordered and repeatable;

3. Set: non-repeatable;

4. Map: unordered, with unique keys and non-unique values.

The above is the detailed content of How to use lockInterruptibly in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template