In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Netty distributed high-performance tool class recycler how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will get something after reading this Netty distributed high-performance tool class recycler article, let's take a look at it.
The use of recycler
This section begins to learn about recycler. Recycler is a lightweight object recycling bin implemented by netty. In netty, recycler is also used quite frequently.
The function of recycler is to ensure the recycling of objects. Objects can be recycled through recycler after use, and if they need to be used again, they can be taken out of the object pool. There is no need to create new objects every time to reduce the occupation of system resources. At the same time, it also reduces the pressure on gc.
Take a look at an example public class RecyclerDemo {private static final Recycler RECYCLER = new Recycler () {@ Override protected User newObject (Handle handle) {return new User (handle);}}; static class User {private final Recycler.Handle handle; public User (Recycler.Handle handle) {this.handle=handle } public void recycle () {handle.recycle (this);}} public static void main (String [] args) {User user1= RECYCLER.get (); user1.recycle (); User user2 = RECYCLER.get (); user2.recycle (); System.out.println (user1==user2);}}
First, we define a member variable RECYCLER of Recycler, and override the newObject method, that is, the method of creating an object, in an anonymous inner class, which is user-defined.
The new User (handle) returned by newObject indicates that objects can be created in this way when the Recycle Bin does not have such objects.
Member variable RECYCLER, which can be used to recycle and reuse such objects.
A static inner class User is defined. There is a member variable handle in User, which is assigned a value in the constructor. The function of handle is for object collection.
And a method recycle is defined. The method body collects its own objects through handle.recycle (this). Through this operation, the objects can be recycled into the Recycler.
The above logic is understood first, and then analyzed in detail.
In the main method, get a user through the get method of RECYCLER, and then recycle it
Then take out the object of the recycle bin through the get method, recycle it again, and finally judge whether the object extracted twice is an object, and the final result is output as true.
The above demo can illustrate the recycling function of Recycler.
After a brief introduction to demo, we will analyze the mechanism of Recycler in detail.
In the source code of the Recycler class, we see this logic private final FastThreadLocal threadLocal = new FastThreadLocal () {@ Override protected Stack initialValue () {return new Stack (Recycler.this, Thread.currentThread (), maxCapacityPerThread, maxSharedCapacityFactor, ratioMask, maxDelayedQueuesPerThread);}}
We are no stranger to this piece of logic. In the previous section, we know that it is used to hold thread shared objects, and the shared object here is an object of type Stack.
An array of type DefaultHandle is maintained in each stack to hold recycled objects, and the relationship between stack and threads is shown in the figure:
8-3-1
In other words, in each Recycler, a thread-shared stack is maintained for recycling a class of objects.
Follow the construction method of Stack: Stack (Recycler parent, Thread thread, int maxCapacity, int maxSharedCapacityFactor, int ratioMask, int maxDelayedQueues) {this.parent = parent; this.thread = thread; this.maxCapacity = maxCapacity; availableSharedCapacity = new AtomicInteger (max (maxCapacity / maxSharedCapacityFactor, LINK_CAPACITY)); elements = new DefaultHandle [min (INITIAL_CAPACITY, maxCapacity)]; this.ratioMask = ratioMask; this.maxDelayedQueues = maxDelayedQueues;}
First, we introduce the key properties initialized in several constructors:
The property parent represents the Reclycer object itself
The property thread indicates which thread is currently bound by stack
The attribute maxCapacity represents the maximum capacity of the current stack and the maximum number of elements that can be held by the stack
The property elements, which represents the object stored in the stack, is of type DefaultHandle and can be referenced by an external object to achieve recycling
The property ratioMask is used to control the frequency of object recycling, that is, every time an object is recycled through Reclycer, it is not recycled every time, but is controlled by this parameter.
Attribute maxDelayedQueues, which is a little complicated here. In many cases, objects created by one thread may be released by another thread, while objects released by another thread will not be placed in the stack of the current thread, but will be stored in a data structure called WeakOrderQueue, which also stores DefaultHandle. WeakOrderQueue will store objects created by thread 1 and released in thread 2.
We only need to know that the maxDelayedQueues attribute means that my thread can recycle several other created objects. Assuming that the current thread is thread 1 and maxDelayedQueues is 2, then thread 1 recycles the objects created by thread 2 and the objects created by thread 3, so it is impossible to recycle the objects created by thread 4. Because of maxDelayedQueues2, I can only recycle objects created by two threads
Property availableSharedCapacity, which represents the maximum number of objects created in thread 1 and cached in other threads. Again, the relevant logic will be parsed later.
There are also two properties that are not initialized in the constructor:
Private WeakOrderQueue cursor, prev;private volatile WeakOrderQueue head
This is the equivalent of a pointer to WeakOrderQueue, which is also understood here, and will be analyzed in detail later.
The relationship between objects between stack heterothreads is shown in the figure (brief):
8-3-2
Let's move on to the construction of Recycler, while familiarizing ourselves with the default values for each parameter of stack:
Protected Recycler () {this (DEFAULT_MAX_CAPACITY_PER_THREAD);}
The overloaded constructor is called here, and the parameter DEFAULT_MAX_CAPACITY_PER_THREAD is passed in
The default value of DEFAULT_MAX_CAPACITY_PER_THREAD is 32768, which is initialized in the static block. We can follow it and analyze it ourselves.
This value represents the maximum number of recycled elements in the stack per thread.
Continue with the overloaded constructor protected Recycler (int maxCapacityPerThread) {this (maxCapacityPerThread, MAX_SHARED_CAPACITY_FACTOR);}
Here, the overloaded constructor is called again, and the 32768 and MAX_SHARED_CAPACITY_FACTOR just passed in
The default value of MAX_SHARED_CAPACITY_FACTOR is 2, which is also initialized in the static block. The usefulness of this attribute will be explained later.
Continue to follow the construction method:
Protected Recycler (int maxCapacityPerThread, int maxSharedCapacityFactor) {this (maxCapacityPerThread, maxSharedCapacityFactor, RATIO, MAX_DELAYED_QUEUES_PER_THREAD);}
The overloaded constructor is also called here, passing in 32768 and 2, as well as two properties, RATIO and MAX_DELAYED_QUEUES_PER_THREAD
RATIO is also initialized in static, with a default value of 8
As above, the default value of MAX_DELAYED_QUEUES_PER_THREAD is 2 times the number of cpu cores.
Let's continue with the construction method:
Protected Recycler (int maxCapacityPerThread, int maxSharedCapacityFactor, int ratio, int maxDelayedQueuesPerThread) {ratioMask = safeFindNextPositivePowerOfTwo (ratio)-1; if (maxCapacityPerThread
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.