current position：Home>[Java Interview] After 7 years of work, I went to the byte interview and turned around on this question. Could you please tell me your understanding of the time wheel?
[Java Interview] After 7 years of work, I went to the byte interview and turned around on this question. Could you please tell me your understanding of the time wheel?
2022-08-06 17:39:13【Follow Mic to learn architecture】
Hello everyone, I'm Mic, a Java programmer who has no talent and can only rely on his looks.
A programmer who has been working for 7 years, went to byte interview and was asked the question of the time round.
He came back from the interview and told me that this question was beyond his knowledge. He also found some articles on the Internet to study, but his understanding was not very deep.
Want me to write an interview essay about the time wheel problem.
Who told me to be so kind?Immediately arranged for this fan.
About the question "What is the time wheel, please tell us your understanding of the time wheel",
I put the master's answers into a 10W interview document, you can scan the QR code at the end of the article to get it
Look at the master's answer below
Everyone remember to like, favorite and follow!
Partners who need expert interview documents and interview documents can scan the QR code at the bottom of the articlespan>
The time wheel, simply understood, is a ring-shaped array used to store a series of timed tasks. Its entire working principle is similar to the dial of our clock.
It consists of two parts, one is a ring array and the other is a pointer to traverse the ring array.
First, define a fixed-length ring array, and then each element of the array represents a time scale, assuming it is 1s, then if it is an array of length 8, it represents 8 seconds.
Then, there is a pointer that loops through the array in a clockwise direction, advancing one array index every minimum time unit.
This pointer makes one revolution to represent 8 seconds, and two revolutions to represent 16 seconds. Assuming that it starts from 0:00:00, it will reach 0:00:9 seconds after one revolution.
Each element in the ring array is a container used to store timed tasks. When we add a timed task to the time wheel, we will calculate the array index stored in it according to the execution time of the timed task..
It is possible that there are multiple timed tasks on a certain time scale, which will be stored in a doubly linked list at this time.
When the pointer points to an array, the tasks stored in the array will be taken out, and then the linked list will be traversed to run the tasks in it one by one.
If the execution time of a scheduled task is greater than the length represented by the ring array, a lap number can generally be used to represent the delayed execution time of the task.
That is to say, if it is the task to be executed in the 16th second, it means that this task should be executed at the subscript 0 position of the array in the second circle.
The benefits of using a time wheel to manage multiple scheduled tasks are many, and I think there are two core reasons:
Reduce the time complexity of adding and deleting scheduled tasks and improve performance.
It can guarantee that each execution of the timer task is O(1) complexity. In the case of intensive timer tasks, the performance advantage is very obvious.
Of course, it also has drawbacks, for very time-critical tasks, the time wheel is not very suitable, because the accuracy of the time wheel algorithm depends on the granularity of the smallest time unit.Assuming that 1s is used as a time scale, tasks that are less than 1s cannot be scheduled by time rounding.
The time wheel algorithm is used in many places, such as Dubbo, Netty, Kafka, etc.
The time wheel algorithm is an interesting design.
It is widely used, but in practical applications, most students have very little contact.
I think this design idea or this data structure can also be used for reference in some specific scenarios in our practical application.
Such as timing retry, decay retry, etc.
Also, I made all Java interview series into full interview documents.Its convenience is that you can find the interview questions you want by searching. Currently, 180 issues have been updated, with a total of more than 15W words!
[Partners who want to receive interview documents can click the business card at the bottom of the article to give it to everyone for free!]
Partners who need expert interview documents can scan the QR code below
author[Follow Mic to learn architecture],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Is the "People's Hybrid" fuel efficient?Test drive Wuling Xingchen hybrid version
- Parallel import Toyota rand cool LuZe Middle East limited-time offers 480000 welcome try-out driving
- How is it convenient to take a car in Wawu Mountain?
- Beijing's passenger car index declaration for the second half of the year starts today, authoritative answers to ten hot issues
- Powerful aura, the new Trumpchi M8 flagship version teaser image released
- Tesla announces it has produced more than 3 million electric cars
- Hybrid cloud storage ignites the trillion-dollar autonomous driving market and accelerates product launch
- Strength "Rolling King" Dreamer
- Remind the car owners to pay attention to the behavior of electric vehicles, which has been clearly prohibited in Anhui, Guangdong and other places.
- The Boyue L is here, and the "chaotic" war begins
guess what you like
Pre-sale starts at 530,000 yuan!The domestic new EQE is off the assembly line, and Mercedes-Benz will launch 8 new energy products in China this year.
Raelfa's big thing is bad, ZEEKR 009 body details exposed_Sohu car_Sohu
Post-70s and post-90s-The difference between car selection_Sohu Auto_Sohu.com
Geely Jiaji Daily Practicality Test Report_Sohu Auto_Sohu.com
Four common driving habits determine your stability
Geely's new Boyue L released, benchmarking Haval H6 and BYD Song
Changing lanes and overtaking China's new energy vehicle brand shortlisted into the world's top 500
Shaanxi: Suspected to be urged by the horn, the owner of the electric car parked the car on the road, blocking many cars – yqqlm
BYD enters the top 500, and new energy and environmental protection travel is worth having_Sohu Auto_Sohu.com
Near-perfect compact SUV test drive BYD Yuan PLUS
- Behind the Besturn B70 five-star safety certification is not only the strict pursuit of driving safety details
- Hangzhou Ferrari 812 is on sale now with a discount of up to 380,000 yuan
- Released a white paper on electric vehicle charging and swapping technology
- In August, Guangzhou plans to configure 46,077 incremental indicators for small and medium-sized passenger cars
- Hangzhou Mercedes-Benz G-Class European version starts at 1.67 million
- The danger signal of new energy vehicles!
- Mercedes-Benz GLE latest market price from 700,000 yuan
- Hangzhou Lexus LX Plus version is reduced by 612,000, and the lowest price is only 1.44 million
- Mercedes-Benz V-class European version is currently priced at 890,000. Welcome to the store for a test drive
- Innovating out of the circle, Jihu teamed up with seven top rock bands to ignite the audience and convey spiritual power_Sohu Automobile_Sohu Network
- Hangzhou Mercedes-Benz GLS US version of the new group purchase maximum discount of 245,000 sold nationwide
- Exclusive identification is very fox two first-team guoan edition models
- The front face is equipped with an exaggerated "big mouth". The new Roewe RX5 is listed, starting from 117,900, with NGP function.
- Suzuki's fifth-generation Wagon is here!Pre-sale 56,000 equipped with 1.0 hybrid, fuel consumption 4L, the appearance is really individual_Sohu Auto_Sohu.com
- Fairytale dream!Extreme Fox Alpha T The Little Prince Edition debuts
- Rand cool LuZe Middle East version 1.05 million welcome opportunities of group purchase price
- Hangzhou Mercedes-Benz CLA's new price starts from 300,000
- Hangzhou parallel import Toyota Sienna US version price 530,000 welcome test ride test drive
- The third-generation Roewe RX5 is here, high-end and cost-effective, not in conflict
- California DMV accuses Tesla of falsely promoting Autopilot and Full Self-Driving features
- The most powerful Volkswagen CC, with a wilder appearance
- Toyota CROWN "Lighting Effect" Display
- The new Volkswagen Taigo overseas version is definitely a boy's favorite
- Mitsubishi "succeeds again", with sufficient aura and a significant improvement in overall texture
- The public has won again, and the big space does not lose to the sound of nature. It is definitely the favorite of boys.
- Santana turned into an SUV with a wheelbase of 2.56 meters
- As soon as it debuted, it became a "popular model", and the design was full of oriental artistic conception, so fragrant!
- It is worthy of being "made by Suzuki", the interior is very technical and the details are very avant-garde
- The new six-wheel drive Mercedes-Benz G63 is unveiled, full of technology, do you like it?
- The pickup version of the Toyota Land Touring debuted, with a futuristic appearance and a high-tech interior
- Real shot of Infiniti Q50, appearance, configuration and comfort are online at the same time
- French car "turn over", the interior style is very young
- Nissan finally "enlarges the move", the exterior and interior styles have been greatly changed, and the power is very rich
- Three-dimensional lines + Hybrid 2.0, a handsome, fun and family-friendly car
- The new Ford Focus overseas real shot, the new image makes consumers shine
- Suzuki Vitra "resurrection", appearance, configuration, and pinpointing it in place
- BYD's new SUV exposed, very suitable for young families
- Korean version of "Haval Kugou" unveiled
- Real shot of imported Touareg off-road vehicle
- Real shot of the new Ford Everest, the exterior style is very aggressive, the interior is very personalized