emmmm,《go web programing》(by SAU SHEONG CHANG)这本书还是不错的,前面八章自己嫌写笔记慢,就走马观花地看过了。没想到,现在回看,没有一点印象。
还是老实记笔记吧,一方面可以拖慢速度,加深对内容的理解;另一方面把一些知识点转化为中文,方便后面查证。emmm,其实是被老马一语惊醒,回头老老实实学 golang。
本文是 《go web programing》的第九章 - Leveraging go concurrency,讨论在 web 开发中,如何利用 golang 独特的并发特性进行并发编程,解决相关问题。
9.1 并发并不是并行 Concurrency isn’t parallelism
实在懒得翻译,择抄下重要句子:
- Concurrency is when two or more tasks start, run, and end within the same period of time and these tasks can potentially interact with each other. The tasks are considered to be concurrent to each other, as opposed to being sequential.
- In parallelism, tasks start and are executed at the same time. Usually a larger problem is split into smaller chunks and processed simultaneously to improve performance.
- Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once — by Rob Pike, co-creator of Go
- Go’s support for concurrency is supported by two main constructs—goroutines and
channels.
9.2 Goroutines
- Goroutines are functions that run independently with other goroutines. This might seem similar to threads—and in fact, goroutines are multiplexed on threads—but they aren’t threads.
- A lot more goroutines than threads can be running, because goroutines are lightweight. A goroutine starts with a small stack (8 K as of Go 1.4) and it can grow (or shrink) as needed.
- Whenever a goroutine is blocked, it blocks the OS thread it’s multiplexed on, but the runtime moves other goroutines on the same blocked thread to another unblocked thread.
9.2 Using goroutines
上代码, goroutine.go:
1 | package main |
goroutine_test.go
1 | package main |
shell 运行:
1 | $ go test -v |
从输出可以看出,第二个测试 TestGoPrint1 没有输出就返回了。我们在 TestGoPrint1 中添加一点等待
1 | func TestGoPrint1(t *testing.T) { |
记得在前面import “time”,再次运行:
1 | $ go test -v |
居然两个测试的输出是一样的,这是因为 printNumbers1 和 printLetters1 的运行非常快,它们无论是串行先后运行,还是并行独立运行,几乎没有差别。