LockSupport and aqslocksupport

Source: Internet
Author: User

LockSupport and aqslocksupport


LockSupport is described in the JDK source code as follows: the basics of building locks and other synchronization classesThread BlockingPrimitive to build a more advanced synchronization tool set. The park/unpark provided by LockSupport blocks and wakes up from the thread granularity. The park/unpark model truly decouples the synchronization between threads, no Object or other variables are required between threads to store the state.

This article starts with the meaning of blocking wake-up, explains the internal mechanism and attention of LockSupport, and finally compares it with the wait and policy of the Object, including the following:

  • Semantics of blocking and Wakeup
  • License Mechanism
  • Underlying implementation
  • Usage
  • What is the difference between wait and policy of an Object?
Blocking Semantics

Blocking means a thread stops running temporarily before it meets certain conditions, and passively releases CPU resources. A thread in this status does not actively enter the thread queue to wait for CPU resources. Instead, the thread needs to wait until the conditions are met before it can re-enter the thread queue to wait for CPU resources. Generally, thread blocking is caused by waiting for an exclusive lock held by other threads, waiting for the end of a certain operation, and waiting for a certain period of time. The thread is suspended after being blocked.BLOCKED,WAITING,TIMED_WAITING. After a thread is blocked, CPU resources are allocated. This is an optimization of busy waiting (busy wait) to avoid wasting too many CPU resources in spin.

License Mechanism

LockSupport uses the permit mechanism to block and wake up threads using park/unpark. A license is a switch that allows the thread to continue execution. When the switch is disabled, the thread will be blocked. When the switch is turned on, the thread will be executed immediately.

Park indicates that the thread will pause execution before obtaining the license (blocking the execution before obtaining the license ). Here the "License" is associated with a thread, similar to a binary semaphore. It cannot be superimposed and only one thread can have one. Some articles describe that "License" is one-time. For example, when thread A calls park, A "License" is consumed (only one "License" is allowed at most "), before unpark is called to release the "License" of thread A, thread A will block obtaining the "License" when calling the park again ". Refer to Understanding JVM Thread States below

there can be only one permit per thread, when thread consumes the permit, it disappears.

For the purpose of thread scheduling, park calling will be blocked until the license is available. If the license is available, the park will be called and will be returned immediately. The current thread will be blocked until the unpark method is called to release the license. Because the license is occupied by default, the current thread will not be able to obtain the license if it calls park after it is started, so it will enter the blocking status.

Underlying implementation

LockSupport is implemented using the Unsafe park. HotSpot Parker maintains a _ counter variable with condition and mutex. When the Variable _ counter is set to 0 and unpark, variable _ counter is set to 1. The park operation checks whether the value is 1. If the value is 1, the system returns the result directly. If the value is not 1, the operation is blocked.

Usage

The following code shows an easy-to-understand example of park/unpark:

1234567891011121314 Public static void main (String [] args) throws InterruptedException {Thread threadA = new Thread (new Runnable () {@ Override public void run () {System. out. println ("I am playing games after the weekend"); LockSupport. park (); System. out. println ("walking around with my girlfriend") ;}}); threadA. start (); Thread. sleep (1, 3000); System. out. println ("my girlfriend is planning to call her boyfriend shopping"); LockSupport. unpark (threadA );}

 

When the thread tries to obtain the permission when executing the operation on Row 3 park, because the thread threadA has obtained the permission by default after startup, the park must wait for the permission to be released before execution. When the main thread calls the unpark method to release the threadA permission, threadA can continue to execute row 7th.

Differences from wait and Policy

Park/unpark and wati/notify both provide the function of blocking and wakeup, which is used for synchronization between threads. However, the granularity of the two is different. park/unpark is used on the thread, while wait/notify works on objects, there is no intersection between them. You must obtain the monitor of the Object before using wait/notify of the Object, but park/unpark does not.

It is not easy to write, painful and happy; understanding may be biased, sentence-based scrutiny; resisting plagiarism, and practicing the path of original technology. If this article can help you, I am Ge Yifan.

Original
Public Account

Reference

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.