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

How to use multithreading and locks

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use multithreading and locks". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

Do you only study for work?

If programming is simply to undertake product requirements development system functions, then the basic program development can be simply understood as according to the requirements of the PRD, defining properties, creating methods, calling display, these three steps.

Especially in some large companies, there will be easy-to-use, perfect and standard architecture and operation and maintenance services, such as: RPC, MQ, Redis cluster, distributed tasks, configuration center, sub-database and sub-table components, gateways and other system architecture. Therefore, programmers only care about business function development!

Let programmers only care about business development, have a mature system architecture, a standard development process, and a general functional design, which is a very good thing for the team to improve effectiveness. But it is precisely because of such a good thing that some programmers do the same thing day after day and end up becoming tool people.

If it is the existence of frameworks and middleware, it is to let programmers only care about business development. So why are you asked about the design and principle of the core components during the interview? In this day and age, don't give up learning is almost the only way to survive.

Second, multithreading and locks have not been used?

Interview must ask multithreading, lock, and maybe even ask quite in-depth, such as: AQS, CAS, CLH, MCS, lock upgrade, object header and so on. But in the actual business development, do you use it? Perhaps this is also the place where most of the students said that the interview was to build rockets!

In the development of some business scenarios in Internet applications, it is true that multithreading is rarely used and you hardly need to lock it. Even where you can use multithreading, you can handle it in other better ways, just as you need multiple threads to store data, so you can use asynchronous MQ to spread the pressure across application instances. The evolution of this development mode is because the current application development and deployment are based on distributed ideas, so it is rare to use threads to squeeze single instance CPU.

Based on RPC+MQ+ database routing + gateway, as well as all kinds of cooperative components, the distributed application has changed our development mode at some time. It may be that we need to use a lot of development ideas of multithreading under a single instance, but after using distributed architecture, we need to change this idea, so there are fewer scenarios that use multithreading and locks at any time.

Figure 14-1 distributed and simplified application deployment

Figure 14-1 distributed and simplified application deployment

However, there is no business scenario of multithreading and locking, for example, in our core components, the use of multithreading and locking is involved in database connection pooling and distributed tasks. There are also some similar commodity second-kill scenarios, which also need to use locks.

Then, the use of multithreading in order to make greater use of resources to improve efficiency, locking is to ensure the correctness of the business process in the case of competition for the same resource. For example: database connection pool in order to allocate database resources reasonably, commodity second kill is for inventory competition.

However, multithreading is not generally used in distributed scenarios where there is no need to compete and allocate resources. If we do a single count of user resources, then the original application is a single instance or can be locked cumulative count. But now is the distributed application deployment, that is, you may be the An instance to provide your needs, when you refresh the page again, you may access the B instance. At this time, it is not so convenient to think about adding up some examples.

This is why there are not many places where you can use multithreading and locks in distributed application frameworks. But if you need to understand the design of some middleware or core components, you need to know the relevant core knowledge.

A lot of skills on paper, that is, you build wheels, rockets, and become the foundation of an architect! If you want to go further along this road, you need to continue to study.

Third, what is your growth goal?

Figure 14-2 the stage of your growth

As far as the path of programming development is concerned, the goal of each growth stage will have its insurmountable difficulties.

When I was in school, it was very difficult to get the sudden strange knowledge running on my own computer.

Work for 1-3 years, before mastering is fur, the next need to have in-depth study, and in-depth will be head-to-head with mathematics.

Working for 3-5 years, it is not so difficult to read previous theoretical knowledge, but how to actually solve some complex projects, or concentrate on the brain stem.

Work for 5 to 7 years, salary and position will become a very difficult to break through the bottleneck at this stage, insufficient accumulation, precipitation is not enough, the current situation is dissatisfied!

After working for 7 to 10 years, I used to find everything difficult to learn, but now it may make it difficult for you to have free time. You don't have to be old enough to have your ability.

With the growth of age, each stage is difficult to overcome. And those who seem to have broken through the bottleneck and reached the height you want. In fact, at every stage, they are in front of them.

But in terms of simple technological growth, in fact, theoretical knowledge is not difficult, as long as you learn, you can still know it, but the time cost is different. But after passing the level of theoretical knowledge, the next thing you have to face is creativity, which is why you feel like you know so much technical content, but you always feel like you can't write good code when you're actually developing.

Know the core technology but can not write good code, very much like: can Chinese characters but can not write poetry, understand color but can not draw mountains and rivers, can jump but can not dance swaying posture.

Therefore, more practical project code and more design patterns will give you a better understanding of how the code should be used, and will be able to break through the current phase barrier. ?? Recommend Brother Fu's "relearn Java Design pattern", official account: bugstack wormhole stack, reply: design pattern, download.

Fourth, how to grow into an architect?

Figure 14-3 architect knowledge system

When it comes to architects, it's really hard to become an architect just by signing up for a course. The growth of architects depends more on whether your R & D team needs an architect, but also requires you to play a due role in this position.

If you are not an architect yet, but want to be an architect. It also depends on whether your boss is willing to train you to be an architect and whether you have a wide range of abilities. In addition, senior developers are not necessarily inferior to architects. Senior developers are sometimes more focused and core than what architects do.

So what are the details other than the overview of the architect's capabilities in figure 14-3?

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

The specification is determined and the architecture is designed.

With a certain depth and breadth of technology, it can change the bug and deal with accidents.

With a team to promote the landing of the project, can also cooperate with other groups.

Understand operation and business planning and get involved in the product development phase in advance.

Understand the business and operation, understand the data indicators and various ROI.

Architecture is more of a combination of experience and experience than a single channel of individual content.

It is not that there is no architecture without an architect. Sometimes a company or team undertakes a project that is not that large, just use the shaping architecture pattern.

However, if there is a very complex scenario design, which is the grouping development of more than a dozen systems, providing services, supporting tens of thousands of seconds kill, hundreds of thousands of daily active users, and expanding to millions of DAU, you need an architect to control it.

Another example: from issuing orders, to trading, to payment, to settlement, to activities, to play, how to support. The complexity of this mass requires an architectural tradeoff.

There is no absolute right or absolute wrong, just when it is more appropriate. Learn more, don't set boundaries for yourself, it's better to break through!

To do a good job in the structure is to see the efficiency of the department from a distance, and to take a closer look at it to solve the bad code! In many cases, the rush may make the whole project rot. More and more bad, will eventually affect the development of the business. So how did all this bad code come from?

Bug often takes over bad code or other people's ideas do not continue to inherit.

Business requirements are simply written at the beginning without scalability, and then continue to pile up.

There is no good structure and naming, and it is never formatted.

The future business trend is not expected, and a reasonable expansibility system cannot be designed.

Show off skills are greater than the overall planning and design, the introduction of a new skill, but the lack of a corresponding match.

There is no design, the functions are all flow programs, write ifelse if you need anything.

Always want a shuttle, it doesn't matter, there are complaints, the department is eager for quick success and instant benefits, do not lay the groundwork for you for a long time, no one to take, can not write good things.

The group lacks corresponding process specifications and reviews, design reviews, code reviews, and no benchmarking projects for reference.

Knowing a few jdk source code is never a basic skill in writing good code. Just like an old carpenter uses an axe and a new carpenter uses a chainsaw, but some of the things made are good and some are not.

There is no always good code, if the code is better, it needs to be maintained and modified all the time.

There is no corresponding volume of business, do not talk about QPS, TPS, TP99, TP999, service health, a lot of empty talk is playing hooligans.

Bad, it comes from many aspects, and it's not something you can learn by signing up for a course. Business, product, R & D, tripartite efforts can better reduce the occurrence of bad, and these are the direction that every R & D should strive for, and it is almost the only way for you to become an architect.

This is the end of "how to use multithreading and locks". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 236

*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