Go语言并发编程,进入并发编程,并发编程原理和相关术语介绍

概述

Meet Jane Sutton. Jane has been working at HSS International Accountancy as a software developer for three months. In her latest project, she has been looking at a problem in the payroll system. The payroll software module runs at the end of the month after the close of business, and it computes all the salary payments for the HSS clients’ employees. Jane’s manager has arranged a meeting with the product owner, the infrastructure team, and a sales representative to try to get to the bottom of the problem. Unexpectedly, Sarika Kumar, CTO, has joined the meeting room via video call.

这是简·萨顿。简在HSS国际会计公司做软件开发已经三个月了。在她最近的项目中,她一直在关注工资系统中的一个问题。工资单软件模块在每月月底结束营业后运行,计算HSS客户员工的所有工资支付。简的经理已经安排了一次会议,与会者包括产品负责人、基础设施团队和一名销售代表,目的是弄清问题的根源。出乎意料的是,首席技术官Sarika Kumar通过视频电话加入了会议室。

Thomas Bock, the product owner, starts: “I don’t understand. The payroll module has been working fine for as long as I can remember. Suddenly, last month, the payment calculations weren’t completed on time, and we got loads of complaints from our clients. It made us look really unprofessional to Block Entertainment, our new and biggest client yet, with them threatening to go to our competitor.”
产品负责人托马斯·博克(Thomas Bock)开始说:“我不明白。从我记事起,工资模块就一直工作得很好。突然,上个月,付款计算没有按时完成,我们收到了客户的大量投诉。这让我们在Block Entertainment(我们最大的新客户)面前显得非常不专业,因为他们威胁要投奔我们的竞争对手。”

Jane’s manager, Francesco Varese, chimes in: “The problem is that the calculations
are too slow and take too long. They are slow because of their complex nature, considering many factors such as employee absences, joining dates, overtime, and a thousand other factors. Parts of the software were written more than a decade ago in C++.
There are no developers left in the firm who understand how this code works.”
简的经理弗朗西斯科·瓦雷泽(Francesco Varese)插话说:“问题在于计算
速度太慢,耗时太长。考虑到许多因素,如员工缺勤、入职日期、加班和其他上千个因素,它们之所以进展缓慢,是因为它们的复杂性。部分软件是十多年前用c++编写的。
公司里已经没有理解这些代码如何工作的开发人员了。”

“We’re about to sign up our biggest client ever, a company with over 30,000 employees. They’ve heard about our payroll problem, and they want to see it resolved before they proceed with the contract. It’s really important that we fix this as soon as possible,” replies Rob Gornall from the Sales and Acquisitions department.
“我们即将签下有史以来最大的客户,一家拥有3万多名员工的公司。他们听说了我们的工资问题,他们希望在签订合同之前先解决这个问题。我们尽快解决这个问题真的很重要,”销售和收购部门的Rob Gornall回答道。

“We’ve tried adding more processor cores and memory to the server that runs the module, but this made absolutely no difference. When we execute the payroll calculation using test data, it’s taking the same amount of time, no matter how many resources we allocate. It’s taking more than 20 hours to calculate all the clients’ payrolls, which is too long for our clients,” continues Frida Norberg from Infrastructure.
“我们已经尝试在运行模块的服务器上添加更多的处理器内核和内存,但这绝对没有什么不同。当我们使用测试数据执行工资单计算时,无论我们分配多少资源,它所花费的时间都是一样的。计算所有客户的工资单需要20多个小时,这对我们的客户来说太长了,”基础设施部门的弗里达·诺伯格继续说道。

It’s Jane’s turn to finally speak. As the firm’s newest employee, she hesitates a little but manages to say, “If the code is not written in a manner that takes advantage of the additional cores, it won’t matter if you allocate multiple processors. The code needs to use concurrent programming for it to run faster when you add more processing resources.”
终于轮到简发言了。作为公司的新员工,她犹豫了一下,但还是说:“如果代码没有充分利用额外的核心,那么即使分配多个处理器也没关系。代码需要使用并发编程,以便在添加更多处理资源时运行得更快。”

Everyone seems to have acknowledged that Jane is the most knowledgeable about the subject. There is a short pause. Jane feels as if everyone wants her to come up with some sort of answer, so she continues. “Right. Okay. I’ve been experimenting with a simple program written in Go. It divides the payroll into smaller employee groups and then calls the payroll module with each group as input. I’ve programmed it so that it calls the module concurrently using multiple goroutines. I’m also using a Go channel to load-balance the workload. At the end, I have another goroutine that collects the results via another channel.”
每个人似乎都承认简对这门学科的知识最渊博。有一个短暂的停顿。简觉得好像每个人都希望她想出某种答案,所以她继续说下去。“对了。好吧。我一直在尝试用Go写一个简单的程序。它将工资单划分为更小的员工组,然后使用每个组作为输入调用工资单模块。我已经对它进行了编程,以便它使用多个线程程序并发地调用模块。我还使用Go通道来负载平衡工作负载。最后,我有另一个程序,通过另一个通道收集结果。”

Jane looks around quickly and sees blank looks on everyone’s faces, so she adds, “In simulations, it’s at least five times faster on the same multicore hardware. There are still a few tests to run to make sure there are no race conditions, but I’m pretty sure that I can make it run even faster, especially if I get some help from accounting to migrate some of the old C++ logic into clean Go concurrent code.”
简迅速环顾四周,看到每个人脸上都是茫然的表情,于是她补充道:“在模拟中,在同样的多核硬件上,它至少快了五倍。还有一些测试需要运行,以确保没有竞争条件,但我很确定我可以让它运行得更快,特别是如果我得到会计的帮助,将一些旧的c++逻辑迁移到干净的Go并发代码中。”

Jane’s manager has a big smile on his face now. Everyone else in the meeting seems surprised and speechless. The CTO finally speaks up and says, “Jane, what do you need to get this done by the end of the month?”
简的经理脸上露出了灿烂的笑容。会议上的其他人似乎都很惊讶,说不出话来。首席技术官终于开口说:“简,你需要什么才能在月底前完成这项工作?”

Concurrent programming is a skill that is increasingly sought after by tech companies. It is a technique used in virtually every field of development, from web development to game programming, backend business logic, mobile applications, crypto, and many others. Businesses want to utilize hardware resources to their full capacity, as this saves them time and money. To accomplish this, they understand that they have to hire the right talent—developers who can write scalable concurrent applications.
并发编程是一种越来越受科技公司追捧的技能。它是一种几乎用于所有开发领域的技术,从web开发到游戏编程、后端业务逻辑、移动应用程序、加密等等。企业希望充分利用硬件资源,因为这样可以节省时间和金钱。为了实现这一点,他们明白他们必须雇佣合适的人才——能够编写可伸缩并发应用程序的开发人员。

关于并发

In this book, we will focus on principles and patterns of concurrent programming. How can we program instructions that happen at the same time? How can we manage concurrent executions so they don’t step over each other? What techniques should we use to have executions collaborate toward solving a common problem? When and why should we use one form of communication over another? We will answer all these questions and more by making use of the Go programming language. Go gives us a full set of tools to illustrate these concepts.
在本书中,我们将关注并发编程的原则和模式。我们如何对同时发生的指令进行编程?我们如何管理并发执行,使它们不会相互重叠?我们应该使用什么技术来让执行协作解决共同的问题?什么时候以及为什么我们应该使用一种沟通方式而不是另一种?我们将通过使用Go编程语言来回答所有这些问题。Go为我们提供了一套完整的工具来说明这些概念。

If you have little or no experience in concurrency but have some experience in Go or a similar C-style language, this book is ideal. This book starts with a gentle introduction to concurrency concepts in the operating system and describes how Go uses them to model concurrency. We’ll then move on to explain race conditions and why they occur in some concurrent programs. Later, we’ll discuss the two main ways we can implement communication between our executions: memory shar

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源滚滚AI编程

创业不易,请打赏支持我一点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值