学习笔记根据 无闻 go语言基础教程 整理
concurrency
- 很多人都是冲着 Go 大肆宣扬的高并发而忍不住跃跃欲试,但其实从源码的解析来看,goroutine 只是由官方实现的超级”线程池”。
- 不过话说回来,每个实例 4-5KB 的栈内存占用和由于实现机制而大幅减少的创建和销毁开销,是制造 Go 号称的高并发的根本原因。
- 另外,goroutine 的简单易用,也在语言层面上给予了开发者巨大的便利。
- 并发不是并行:Concurrency Is Not Parallelism
- 并发主要由切换时间片来实现“同时”运行,在并行则是直接利用
- 多核实现多线程的运行,但 Go 可以设置使用核数,以发挥多核计算机的能力。
- Goroutine 奉行通过通信来共享内存,而不是共享内存来通信。
channel
- Channel 是 goroutine 沟通的桥梁,大都是阻塞同步的
- 通过 make 创建,close 关闭
- Channel 是引用类型
- 可以使用 for range 来迭代不断操作 channel
- 可以设置单向或双向通道
- 可以设置缓存大小,在未被填满前不会发生阻塞
select
- 可处理一个或多个 channel 的发送与接收
- 同时有多个可用的 channel时按随机顺序处理
- 可用空的 select 来阻塞 main 函数
- 可设置超时
示例代码
package main
import (
"fmt"
"time"
"runtime"
"sync"
)
func main() {
test1T()
}
func test1() {
fmt.Println("Go Go Go !!!")
}
func test1T() {
fmt.Println("=============test1T=============")
go test1()
time.Sleep(2 * time.Second)
}
func test2() {
fmt.Println("=============test2=============")
c := make(chan bool)
go func() {
fmt.Println("Go2")
c <- true
}()
<-c
}
func test3() {
fmt.Println("=============test3=============")
c := make(chan bool)
go func() {
fmt.Println("Go2")
c <- true
close(c)
}()
for v := range c {
fmt.Println(v)
}
}
func test4() {
fmt.Println("=============test4=============")
c := make(chan bool)
go func() {
fmt.Println("Go3")
c <- true
}()
<- c
}
func test5() {
fmt.Println("=============test5=============")
c := make(chan bool)
go func() {
fmt.Println("Go3")
<- c
}()
c <- true
}
func test6() {
fmt.Println("=============test6=============")
c := make(chan bool, 1)
go func() {
fmt.Println("Go3")
<- c
}()
c <- true
}
func test7() {
a := 1
for i := 0; i<10000000; i++ {
a += i
}
fmt.Println(a)
}
func test7T() {
fmt.Println("=============test7T=============")
for i := 0; i < 10; i++ {
go test7()
}
}
func test8(c chan bool, index int) {
a := 1
for i := 0; i<100; i++ {
a += i
}
fmt.Println(index, a)
if index == 9 {
c <- true
}
}
func test8T() {
fmt.Println("=============test8T=============")
c := make(chan bool)
for i := 0; i < 10; i++ {
go test8(c, i)
}
<-c
}
func test9T() {
fmt.Println("=============test9T=============")
runtime.GOMAXPROCS(runtime.NumCPU())
c := make(chan bool)
for i := 0; i < 10; i++ {
go test8(c, i)
}
<-c
}
func test10(c chan bool, index int) {
a := 1
for i := 0; i<100; i++ {
a += i
}
fmt.Println(index, a)
c <- true
}
func test10T() {
fmt.Println("=============test10T=============")
runtime.GOMAXPROCS(runtime.NumCPU())
c := make(chan bool, 10)
for i := 0; i < 10; i++ {
go test10(c, i)
}
for i := 0; i < 10; i ++ {
<-c
}
}
func test11(wg *sync.WaitGroup, index int) {
a := 1
for i := 0; i < 100; i++ {
a += i
}
fmt.Println(index, a)
wg.Done()
}
func test11T() {
fmt.Println("=============test11T=============")
runtime.GOMAXPROCS(runtime.NumCPU())
wg := sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go test11(&wg, i)
}
wg.Wait()
}
func test12() {
fmt.Println("=============test12=============")
c1, c2 := make(chan int), make(chan string)
o := make(chan bool, 2)
go func() {
for {
select {
case v, ok := <-c1:
if !ok {
o <- true
break
}
fmt.Println("c1",v)
case v, ok := <- c2:
if !ok {
o <- true
break
}
fmt.Println("c2", v)
}
}
}()
c1 <- 1
c2 <- "hi"
c1 <- 3
c2 <- "hello"
close(c1)
close(c2)
for i := 0; i < 2; i ++ {
<- o
}
}
func test13() {
fmt.Println("=============test13=============")
c := make(chan int)
go func() {
for v := range c {
fmt.Println(v)
}
}()
for i := 0; i < 6; i++ {
select {
case c <- 0:
case c <- 1:
}
}
}
func test14() {
fmt.Println("=============test14=============")
c := make(chan bool)
select {
case v:= <-c:
fmt.Println(v)
case <- time.After(3 * time.Second):
fmt.Println("Timeout After 3 second")
}
}