当前位置:网站首页>Replication of variables in golang concurrency
Replication of variables in golang concurrency
2022-06-24 04:41:00 【jerryteng】
Recently golang Programming language development , Deeply realized golang The convenience of language parallelism , But it also brings some small problems ,
No big problem , Let's solve . Let's first look at what concurrency is .
What is parallel ? What is concurrency ?
- The key to parallelism is that you can do many things at the same time , There are many common parallel computers , Multicore parallelism ; Concurrency is managing a lot of things at the same time , These things can be carried out within the specified time .
- Concurrency focuses on avoiding blocking , So that the program will not stop because of blocking ; Parallelism is dependent on hardware and operating system resources .
- Concurrency lies in structure , Parallelism lies in executing . The application has a good concurrent structure , The operating system can make better use of hardware parallel execution , Dispatch reasonably , promote CPU utilization .
golang in , We all talk about parallel , Try to tap the potential of the machine , Take full advantage of the multi-core nature of the machine , Make code execution most efficient .
golang The built-in functions of the language are used in parallel go , Let's start a collaborative process . This process is called goroutine
What is? goroutine?
goroutine Is a lightweight abstraction based on threads . stay Go In language , When a function is created as goroutine when ,Go Think of it as a separate unit of work , And it can execute multiple in parallel at a very low cost goroutine.
say , Today's problem ,
I'm in a loop , Set up a group goroutinue , Each coroutine executes a function , In this function ,
func (s *ServiceImpl) getData(
ctx context.Context,
aa int64,
where []string,
) (data Data,err error) {
queryWhere := where
I found that every request has a problem ,
Ten pieces of data will be queried each time , But every time you assemble SQL All have problems , There is a problem SQL It's also random .
So I print the log in the code to see ,
Find log output , It turned out to be normal
This is strange , Add a line of code for logging ,
Remove Log code , Another problem 100% Reappear .
Finally, various search problems , Suddenly found that ,queryWhere It's a slice ,
It is still a matter of basic knowledge ,golang in slice At the bottom is an array , And it is a reusable array ,
Change the code to
func (s *ServiceImpl) getData(
ctx context.Context,
aa int64,
where []string,
) (data Data,err error) {
queryWhere := make([]string, len(where))
copy(queryWhere, where)Do not add log statements , The problem has also been solved .
We still have to lay more foundation
边栏推荐
- Brief introduction: how much do you know about supply chain attacks
- How to add a domain name to ECS? What are the advantages of ECS?
- Physicochemical properties and specificity of Worthington trypsin
- Ribbon
- 英特尔 XTU 官方超频工具已支持 Win11 22H2 和 13 代酷睿 Raptor Lake 处理器
- Congratulations to Zhong Jun for becoming the maintainer of chaos metric model working group
- 线性回归的损失和优化,机器学习预测房价
- mysql中表的命名
- DP summary of ACM in recent two weeks
- web渗透测试----5、暴力破解漏洞--(5)SMB密码破解
猜你喜欢

博士申请 | 香港科技大学(广州)刘浩老师招收数据挖掘方向全奖博士/硕士

由浅入深的混合精度训练教程

一款支持内网脱机分享文档的接口测试软件

阿里云新一代云计算体系架构 CIPU 到底是啥?

Jointly build Euler community and share Euler ecology | join hands with Kirin software to create a digital intelligence future

Idea创建Servlet 后访问报404问题

Clang code coverage detection (pile insertion technology)

Idea creates a servlet and accesses the 404 message

SAP mts/ato/mto/eto topic 7: ATO mode 1 m+m mode strategy 82 (6892)

Changjiang Dayong, director of openeuler community: jointly promote the new open source model of Euler and jointly build a new open source system
随机推荐
How to select a suitable optical fiber tester
Beauty of script │ VBS introduction interactive practice
Recognize workplus again, not only im but also enterprise mobile application management expert
How to operate ECs and what types of system configuration schemes can be supported
Web technology sharing | [map] to realize customized track playback
What does IIS mean and what is its function? How does IIS set the size of the web site space on the server?
[2021 "shadow seeking" medical artificial intelligence algorithm competition] Ti-One product use tutorial
Database answers build standard, answer as required
Digital transformation practice of Zheshang Bank
How do ECS create FTP accounts? What should I pay attention to during creation?
Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse
How to build a website for ECS is the price of ECS very expensive
阿里云新一代云计算体系架构 CIPU 到底是啥?
uni-app进阶之认证【day12】
Apipost interface assertion details
黑马程序员机器学习讲义:线性回归api初步使用
I have an agreement with IOT
How to file ECS? What should be paid attention to when selecting ECS
Weak current engineer, 25g Ethernet and 40g Ethernet: which do you choose?
15+ urban road element segmentation application, this segmentation model is enough