Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is SynchronousQueue?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is SynchronousQueue". In daily operation, I believe many people have doubts about what is SynchronousQueue. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is SynchronousQueue?" Next, please follow the editor to study!

For a time, Da Liu often lectured to some programmers. On the one hand, this is due to the needs of team training, on the other hand, Da Liu himself wants to engage in Versailles to show off his strength.

Daliu's lecture allows anyone in the company to go in and listen. Post the theme in the company group a week in advance, and if someone wants to hear about it, just go.

Once, when Da Liu was talking about concurrency topics, in order to show that he was really a developed person, he used a SynchronousQueue as an example. He said that there is no concept of volume in this queue, which means that threads hold data to match each other.

Well, at this point, I still want to say that Da Liu doesn't really know much about SynchronousQueue. It's just that, on the one hand, no one uses it, and naturally no one understands it; on the other hand, its concept is relatively obscure, and sometimes it is counterintuitive, so even if something you say casually may not be right, it may not be discovered. it can also give people a feeling that they don't understand but think it's powerful.

Daliu has used it several times and feels good. Therefore, if you have nothing to do, you have to show SynchronousQueue to show that you also know that you are so remote, and that the name of a developed person is not wrong.

At that time, it happened to be torn down.

At that time, there was a new technique in the class. The man was of medium height and plain, but his face looked like the slap of an old farmer who had been farming for many years. The bumps on his face are like calluses on the palms of an old farmer. The man's surname is Zhang. Here, because his face looks like a big slap, let's call him a slap.

This slap Zhang interrupted Da Liu, saying firmly that what Da Liu said was wrong, saying that he had seen this SynchronousQueue, but it was not what Da Liu said.

Big Liu is a little guilty, his neck oozes a circle of sweat, but the developed person's name Da Liu doesn't want to lose it. So he said a lot of nonsense in the clouds and fog, taking the topic astray. And told Zhang, next time to compete with him on this stage, to see who is the real bosom friend of SynchronousQueue.

Because big Liu felt that he had been slapped in the face, he made up his mind to study SynchronousQueue thoroughly.

Google and Baidu check together, things are combined, foreign things serve China, and they have been doing well for a while. Finally, there was a little broken website in every corner, and someone said something like this:

The purpose of SynchronousQueue is to connect, in order to match, when the connection is connected, the two sides will cooperate happily and the whole work will be completed. But once in the joint, either side has not yet arrived, then the other side must block and wait.

This sentence suddenly knocked open the skull of Da Liu, allowing the smart IQ to reoccupy the highland.

Why did this sentence light up the head of Da Liu, which was already like a light bulb? Because Da Liu remembered his interview experience every time, it was the same as this connection.

Every time Da Liu goes to the interview, he arrives at the new company in advance. But in most cases, it takes a long time to start the interview when the time is up. Big Liu was also young at that time, but thought the leader was busy, so he waited respectfully.

Until Da Liu himself became a leader and went to interview others, he was politely reminded by HR to let the candidate wait for a while, which shows that the company is very busy and makes the candidate maintain a certain degree of awe of the company. At that time, Da Liu knew that this was a PUA technique.

Compared with his interview experience, Da Liu understood the concept of SynchronousQueue at once.

SynchronousQueue itself exists for handover and matching. When a thread puts something on the SynchronousQueue and finds that there is no thread waiting to pick it up, it is blocked-it's like the interviewer coming early and waiting for the interviewer.

When a thread goes to SynchronousQueue to pick up something and finds nothing, it waits-just like the interviewer comes early and waits for the interviewer.

When I understood SynchronousQueue, it was a winter, the cold wind outside the house was shining, and the big Liu inside was shining.

It's just a queue structure in the parallel package at the bottom of JDK. Of course, SynchronousQueue is not that simple, and there are hundreds of millions of details in it.

Therefore, after understanding the overall direction, Da Liu began to study the details. He wants to work hard to put down the arrogance of the slap, and Da Liu wants to be the top card of the company's technology.

Back to reality, the real purpose of SynchronousQueue is to hand over the work results of the two threads. There's nothing wrong with that. However, in this handover, it needs to be kept strictly confidential and no one can spy on it.

Well, yes, you can't be spied on just because you have an appointment with your girlfriend to go to a part-time room.

OK, around this part-time room of SynchronousQueue, let's look at the details through the source code.

First of all, the part-time room is strictly confidential, so how many people are inside can not be known. Therefore, it is impossible for others to get specific data through methods. For SynchronousQueue, naturally, you can't get any information through size ().

/ * Always returns zero. * A {@ code SynchronousQueue} has no internal capacity. * * @ return zero * / public int size () {return 0;} / * * Always returns {@ code true}. * A {@ code SynchronousQueue} has no internal capacity. * * @ return {@ code true} * / public boolean isEmpty () {return true;}

Secondly, the part-time room can not casually go in for rounds to see who it is. So, naturally, you can't iterate.

/ * * Returns an empty iterator in which {@ code hasNext} always returns * {@ code false}. * * @ return an empty iterator * / public Iterator iterator () {return Collections.emptyIterator ();}

Third, the hourly room to protect privacy, it can not let you drill a loophole, do not tell you whether XXX is hiding in the hourly room. So, you can't know if there's this guy in the room at the hour.

/ * * Always returns {@ code false}. * A {@ code SynchronousQueue} has no internal capacity. * * @ param o the element * @ return {@ code false} * / public boolean contains (Object o) {return false;} / * Returns {@ code false} unless the given collection is empty. * A {@ code SynchronousQueue} has no internal capacity. * * @ param c the collection * @ return {@ code false} unless given collection is empty * / public boolean containsAll (Collection c) {return c.isEmpty ();}

Naturally, part-time rooms do not have the right to drive people out.

/ * * Always returns {@ code false}. * A {@ code SynchronousQueue} has no internal capacity. * * @ param o the element to remove * @ return {@ code false} * / public boolean remove (Object o) {return false;}

Of course, as a commercial part-time room, SynchronousQueue still pays great attention to safety, and it intimately provides a means of emergency transfer.

/ * * @ throws UnsupportedOperationException {@ inheritDoc} * @ throws ClassCastException {@ inheritDoc} * @ throws NullPointerException {@ inheritDoc} * @ throws IllegalArgumentException {@ inheritDoc} * / public int drainTo (Collection

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report