Java Concurrency Framework--aqs timeout mechanism

Source: Internet
Author: User

Another excellent mechanism provided by the AQS framework is the support of lock acquisition timeouts, which can cause certain threads to acquire no locks for a long period of time when a large number of threads compete for a lock, and in some scenarios it may be desirable to cancel the wait for the lock to improve performance if the thread fails to acquire the lock for a period of time. This will require a timeout mechanism. There was no Juc tool before JDK1.5, when the concurrency control function implemented the lock through the JVM's built-in synchronized keyword, but was overwhelmed by some special requirements, such as timeout cancellation control. JDK1.5 the introduction of the Juc tool perfectly solves this problem, which is benefiting from the support of the concurrency infrastructure Aqs providing timeouts.
In order to guarantee the accuracy of time interval statistics more precisely, the implementation uses the System.nanotime () more precise method, which can be accurate to the nanosecond level. The idea of a time-out mechanism is to keep track of the competition at the same time as the lock competition, and once the time period exceeds the specified time, stop polling to return directly and cancel the corresponding node in the waiting queue before returning. Looking down on the logic of implementation,
If (attempt to acquire lock failed) {
Long lasttime = System.nanotime ();
Create node
Using CAs to insert node into the tail of the queue
while (true) {
If (attempt to acquire the lock succeeds and node's predecessor is the head node) {
Set the current node as the head node
Jump out of the loop
}else{
if (nanostimeout <= 0) {
Cancel this node in the wait queue
Jump out of the loop
}
Use CAs to modify node precursor nodes ' waitstatus identity as signal
if (modified successfully)
if (Nanostimeout > Spinfortimeoutthreshold)
Block Current thread nanostimeout nanoseconds
Long now = System.nanotime ();
Nanostimeout-= Now-lasttime;
Lasttime = Now;
}
}
Above is in the previous chapter lock acquisition logic to add timeout processing, the core logic is constantly cycle minus processing time consumption, once less than 0 to cancel the node and out of the loop, of which there are two points must be noted that one is the real blocking time should be deducted from the competition in the queue time remaining time, to ensure the accuracy of blocking events, We can see that each cycle is subtracted from the corresponding processing time, and the other is about the Spinfortimeoutthreshold variable threshold, which is the split line that determines whether to use spin-off or use the system blocking mode to consume time. The JUC Toolkit author tests to set the default value to 1000ns, that is, if the remaining time is greater than 1000ns after the successful insertion of the wait queue, call the system's underlying block, or do not call the system bottom, instead of just let it in the Java application layer continuously cycle consumption time, is the measure of optimization.
At this point, the AQS framework provides a time-out mechanism for acquiring locks, and the support for timeouts provides a more sophisticated mechanism for concurrency, and more concurrency strategies to meet the needs of developers.

Java Concurrency Framework--aqs timeout mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.