In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the implementation principle and case of Java multithreading". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Java multithreading implementation principles and cases" it!
Four elements
The four elements of semaphore are: maximum number of permits, fairness mode, acquire method and release method. The maximum number of permissions and fairness mode are specified when the Semaphore object is built, indicating, respectively, how many threads can access common resources at the same time and whether fair mode is used when obtaining permissions. The acquire method is used to obtain a license and enter a waiting state if the license is insufficient. The release method is used to release the license.
The realization of unfair mode
The implementation of Semaphore class is based on AQS synchronizer, both fair mode and unfair mode are based on AQS sharing mode, but the operation logic of obtaining permission is different. The default mode of Semaphore is unfair mode. Let's first look at the implementation of unfair mode. Phellodendron mandshurica (Thunb.)
The main methods of the Semaphore class are shown below, where two constructors are provided, and the two related parameters are the maximum number of permits and whether or not to use fair mode, where FairSync is the synchronizer of fair mode and NonfairSync is the synchronizer of unfair mode. There are two acquire methods, and when there is no parameter, the default is to get one license at a time, while passing in an integer parameter indicates that several licenses are obtained at a time. Correspondingly, there are two release methods, where no parameter means that one license is released, while the integer parameter indicates that several licenses are released at a time. The main methods of Semaphore are as follows
The Sync subclass within Semaphore is the abstract parent class of the fair mode FairSync class and the unfair mode NonfairSync class, and the maximum number of permits corresponds to the state variable of the AQS synchronizer. Because the pattern is unfair, an unfair license acquisition method, nonfairTryAcquireShared, is provided here. The unfair model actually allows all threads to spin when the number of permits permits, regardless of the order in which they come first and arrive later, all threads are put together to compete for licenses. Among them, the compareAndSetState method provides the CAS algorithm to ensure that the license value can be modified concurrently, and the remaining license number is equal to the current available license value minus the current consumption license number. It should be noted that when the remaining license number is less than 0, a negative number is returned, which causes the thread to enter the waiting queue. The tryReleaseShared method provides the operation of releasing a license, regardless of whether it is in fair mode or not, and the logic for releasing a license is the same. Spin operation to increase the number of licenses released to the current number of remaining licenses.
The implementation of the unfair mode NonfairSync class is mainly the tryAcquireShared method, which can be directly called by the nonfairTryAcquireShared method of the parent class Sync. Phellodendron mandshurica (Thunb.)
Phellodendron mandshurica (Thunb.)
Realization of fair mode
The main difference between the fair mode and the unfair model lies in the mechanism of obtaining the license. The unfair mode allows all threads to compete for the license directly through the spin operation, which leads to the unfairness. On the other hand, the fair mode realizes the fair mechanism through queue. The difference between them lies in the tryAcquireShared method. Let's look at the fair mode tryAcquireShared method. In fact, the difference is that two lines of code with a box are added in the following figure, which will check whether there is already a waiting queue. If there is already a waiting queue, return-1, which means that the AQS Synchronizer will put the current thread into the waiting queue, which means fairness. In fact, this is not strictly fair. The fairness section of AQS Synchronizer mentioned earlier has in-depth discussion of AQS fairness. If you forget, you can refer to it again to deepen understanding. And in order to reach the maximum number of permits, all threads do not enter the waiting queue, but all threads spin to obtain permission.
Case 1
Let's look at a simple example: first instantiate a semaphore object with five permissions, then a total of 10 threads try to get five licenses together, and the licensed thread accumulates the value by 1, then sleeps for five seconds, and finally releases the license.
The output of the above program is as follows, and after five threads output "counting number: xx", other threads start to wait. Wait about five seconds for the licensed five threads to perform the release license operation before the other threads can get permission and proceed.
Case 2
Example 2 is very similar to example 1, except that 2 licenses are consumed each time a license is obtained, and 2 licenses are also released when released. Here, instantiate a semaphore object with 6 permissions, and then 10 threads try to get the license together. But this time, you can only get permission for up to three threads at the same time, that is, three threads add up to the value value after getting permission, and then sleep for five seconds and then release the license. Then three other threads are allowed to execute until all 10 threads are finished.
At this point, I believe that everyone on the "Java multithreading implementation principles and cases" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.