In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is blocking". Friends who are interested might as well take a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is blocking"?
1. To interrupt is to stop from interrupting
It's not for the eunuch to work for you. He doesn't have the ability. Eunuchs are used to dispatch your work.
For example, if a rebellious army attacks outside the city and the eunuch reports in a panic, you have to suspend the activities of the harem and deal with the first problem with your trousers; or, for example, there are newly arrived concubines winking frequently, but you still have a lot of official documents to criticize, willing but powerless.
This way of dealing with the problem is to interrupt (from the interruption to the eunuch). Interruption means that during the normal operation of CPU, due to internal and external events or events prearranged by the program, CPU temporarily stops the running program and turns to serve the internal or external event or prearranged event, and then returns to run the temporarily interrupted program after the service is completed.
Let's take a look at the underlying interrupt handler.
Request_irq (unsigned int irq, irq_handler_t handler, unsigned long flags, const char * name, void * dev)
It can be seen that the eunuch only needs to give the emperor what to do, code for the record, fix the processing flow, adjust the priority, and the emperor's time slice can be rotated effectively. So as not to lose all the rivers and mountains, but also in the harem of love.
Take network transmission, for example, when there is a network packet, it needs to be processed in time, otherwise the client will time out. At this time, the network card will immediately issue an interrupt request, and CPU will handle these buffers through the interrupt program of the network card. This is all very important work.
Interrupts can be divided into hard interrupts and soft interrupts. Hard interrupts are generated by hardware, such as disks, network cards, keyboards, clocks, etc. Soft interrupts are generated by processes that are currently running and usually have a lower priority than hard interrupts.
two。 Will blocking take up CPU?
By substituting the status of emperor, we can explain some of the puzzling problems that we usually encounter.
We've all seen a class called LinkedBlockingQeque under the Concurent package. As can be seen from its name, this is a blocking queue. In fact, it doesn't sell dog meat with a sheep's head.
As in the following code, we usually put it in a loop. I have a psychological shadow about while (true), because it may run all over your CPU.
While (true) {Object o = linkedBlockingQeque.poll ();}
But in fact, it won't. Because as they say, this is a blocking queue.
Similarly, there is select in NIO. Aren't you afraid of retribution if you put the logic in the while loop?
While (! stop) {int num = selector.select (); if (num = 0) {continue;} Iterator events = selector.selectedKeys () .iterator ();}
I'm not afraid of that. Because blocking does not take up any resources.
For example, the little eunuch reported a discount about the corruption of Lu Jifei's uncle. But this issue, we need to wait for the results of the judicial investigation, but also need to listen to the opinion of the princess, we can first put it aside.
Write down the problem in another pamphlet, and when the dependent tasks are almost done, and you have dragons, you can continue to deal with them.
As you can see, this blocking problem, although a task, will not take up any of your time, which is the same in the computer.
Let's take a look at common Java blocking methods.
Sleep and wait
Sleep and wait. The use of words is very ingenious. What is the beauty of it? Because it's a real scene.
Sleep
The sleep function puts the thread into a blocking state for a certain amount of time, does not get the cpu time, but does not release lock resources. After the specified time has passed, the thread returns to the executable state.
Note that we are talking about threads, not CPU itself. Just because the thread is inactive doesn't mean CPU can't do anything else.
For example, today is the auspicious day of the zodiac to receive ministers. Wang Tianshi got the opportunity to meet, and other ministers had to wait outside to be summoned. As a result, Wang Tianshi's conversation was so smelly and long that it didn't arouse any interest from you. It just so happens that the little eunuch came in a hurry and whispered in your ear: Li Guifei has given birth to a son!
This is an exhilarating thing because all the other sons were KO in the palace. So you pretended to say to Wang Tianshi: I have a headache and need to take a nap. "in fact, you have secretly gone to visit Li Guifei.
Note that at this time, Wang Tianshi can only wait submissively. For the main line of "interview", other ministers can only wait outside to be subpoenaed. None of them got the lock of "interview", and Wang Tianshi kept it until you finished watching your son's return.
This is what sleep means that the lock is not released, because nothing has changed at the moment of sleep after sleep.
Wait
Wait () puts the thread into a blocking state while releasing the lock resources it owns and is used in conjunction with notify ().
For wait, it's completely different.
As shown in the figure, each Monitor can only be owned by one thread at some point, and that thread is "Active Thread". The other threads are "Waiting Thread", waiting in two queues "Entry Set" and "Wait Set", respectively. The state of the thread waiting in "Entry Set" is "Waiting for monitor entry", while the state of the thread waiting in "Wait Set" is "in Object.wait ()".
The term is difficult to understand, so let's be chic as emperor.
At this time, you are going to meet the minister. However, I don't want to one by one anymore, because it's too inefficient and boring. If a minister stays in your study for a long time, some ministers may suspect that you are doing gay. This side effect is unpleasant.
P2P is no good, so let's get together and have a heart-to-heart talk.
The one who is talking to you is Wang Tianshi, because he talks a lot, and you like him better.
Wang Tianshi said, "the little prince was born on dog days. Let's call him Shi Sanfu."
That's when you remembered your surname Shi. As an emperor who is familiar with the article, you scoff at this, listening to this unfashionable name, but also vaguely angry.
Wang Aiqing, you'd better wait first and listen to other people's opinions.
At this time, a large number of brown-nosing ministers began to raise their hands, eager to try. Liu Daochang snatched the lock on the main line of the conversation.
Liu Daochang: heaven and earth for a long time, people have an end, there is a fish in the north, its name is the fish, can live for 100 million years. I think we'll call it Shi.
After listening to this, you nodded slightly, and sure enough, the fairy's mouth smelled good, but it always felt a little strange.
Pay attention. The group of ministers waiting to speak are called Entry Set, and those who raise their hands quickly can answer this question.
Ministers like Wang Tianshi, who have been stopped, belong to Wait Set. Only if you let him speak again will he have a chance.
During this whole process, the conversation can continue, not because Wang Tianshi has been banned from talking. We can say that the wait operation releases the object lock.
All kinds of so-called blocking in the computer are dealt with by dividing different queue resources. For example, epoll is programmed around work queues and waiting queues. Although the underlying data structures are somewhat different, the ideas are all the same.
How does a thread get a time slice?
This is not easy to answer, because you need to know the fact that threads in Java are essentially lightweight processes on Linux, and their scheduling is done by the operating system.
Take a look at our top picture, which makes people prone to dense phobias. Our CPU time is divided into multiple CPU time slices. Just because your program is executing while (true) doesn't mean it always gets CPU resources, so other processes have the opportunity to execute it.
JVM adopts preemptive scheduling model, which means that threads with high priority occupy more CPU. If threads have the same priority, then randomly select a thread to occupy CPU.
Pay attention to the word "random", it is very magic. It can win 1 million of the lottery every day, or you may choke on drinking water every day.
The poor computer system is also involved in the helpless random fate of the world.
But there is a very domineering task of robbing CPU, and that is the hard interruptions we mentioned above-things that have to be dealt with first.
At this point, I believe you have a deeper understanding of "what is blocking". You might as well do it in practice. 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.