Remove 02.7.md spaces

This commit is contained in:
vCaesar
2017-06-10 11:42:36 +08:00
parent 3cb9ed7598
commit 975a3a3d1c

View File

@@ -9,40 +9,40 @@ goroutine是Go并行设计的核心。goroutine说到底其实就是协程
goroutine是通过Go的runtime管理的一个线程管理器。goroutine通过`go`关键字实现了,其实就是一个普通的函数。 goroutine是通过Go的runtime管理的一个线程管理器。goroutine通过`go`关键字实现了,其实就是一个普通的函数。
```Go ```Go
go hello(a, b, c) go hello(a, b, c)
``` ```
通过关键字go就启动了一个goroutine。我们来看一个例子 通过关键字go就启动了一个goroutine。我们来看一个例子
```Go ```Go
package main package main
import ( import (
"fmt" "fmt"
"runtime" "runtime"
) )
func say(s string) { func say(s string) {
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
runtime.Gosched() runtime.Gosched()
fmt.Println(s) fmt.Println(s)
}
} }
}
func main() { func main() {
go say("world") //开一个新的Goroutines执行 go say("world") //开一个新的Goroutines执行
say("hello") //当前Goroutines执行 say("hello") //当前Goroutines执行
} }
// 以上程序执行后将输出: // 以上程序执行后将输出:
// hello // hello
// world // world
// hello // hello
// world // world
// hello // hello
// world // world
// hello // hello
// world // world
// hello // hello
``` ```
我们可以看到go关键字很方便的就实现了并发编程。 我们可以看到go关键字很方便的就实现了并发编程。
上面的多个goroutine运行在同一个进程里面共享内存数据不过设计上我们要遵循不要通过共享来通信而要通过通信来共享。 上面的多个goroutine运行在同一个进程里面共享内存数据不过设计上我们要遵循不要通过共享来通信而要通过通信来共享。
@@ -57,41 +57,41 @@ goroutine是通过Go的runtime管理的一个线程管理器。goroutine通过`g
goroutine运行在相同的地址空间因此访问共享内存必须做好同步。那么goroutine之间如何进行数据的通信呢Go提供了一个很好的通信机制channel。channel可以与Unix shell 中的双向管道做类比可以通过它发送或者接收值。这些值只能是特定的类型channel类型。定义一个channel时也需要定义发送到channel的值的类型。注意必须使用make 创建channel goroutine运行在相同的地址空间因此访问共享内存必须做好同步。那么goroutine之间如何进行数据的通信呢Go提供了一个很好的通信机制channel。channel可以与Unix shell 中的双向管道做类比可以通过它发送或者接收值。这些值只能是特定的类型channel类型。定义一个channel时也需要定义发送到channel的值的类型。注意必须使用make 创建channel
```Go ```Go
ci := make(chan int) ci := make(chan int)
cs := make(chan string) cs := make(chan string)
cf := make(chan interface{}) cf := make(chan interface{})
``` ```
channel通过操作符`<-`来接收和发送数据 channel通过操作符`<-`来接收和发送数据
```Go ```Go
ch <- v // 发送v到channel ch. ch <- v // 发送v到channel ch.
v := <-ch // 从ch中接收数据并赋值给v v := <-ch // 从ch中接收数据并赋值给v
``` ```
我们把这些应用到我们的例子中来: 我们把这些应用到我们的例子中来:
```Go ```Go
package main package main
import "fmt" import "fmt"
func sum(a []int, c chan int) { func sum(a []int, c chan int) {
total := 0 total := 0
for _, v := range a { for _, v := range a {
total += v total += v
}
c <- total // send total to c
} }
c <- total // send total to c
}
func main() { func main() {
a := []int{7, 2, 8, -9, 4, 0} a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int) c := make(chan int)
go sum(a[:len(a)/2], c) go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c) go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c x, y := <-c, <-c // receive from c
fmt.Println(x, y, x + y) fmt.Println(x, y, x + y)
} }
``` ```
默认情况下channel接收和发送数据都是阻塞的除非另一端已经准备好这样就使得Goroutines同步变的更加的简单而不需要显式的lock。所谓阻塞也就是如果读取value := <-ch它将会被阻塞直到有数据接收。其次任何发送ch<-5将会被阻塞直到数据被读出。无缓冲channel是在多个goroutine之间同步很棒的工具。 默认情况下channel接收和发送数据都是阻塞的除非另一端已经准备好这样就使得Goroutines同步变的更加的简单而不需要显式的lock。所谓阻塞也就是如果读取value := <-ch它将会被阻塞直到有数据接收。其次任何发送ch<-5将会被阻塞直到数据被读出。无缓冲channel是在多个goroutine之间同步很棒的工具。
@@ -99,24 +99,24 @@ channel通过操作符`<-`来接收和发送数据
上面我们介绍了默认的非缓存类型的channel不过Go也允许指定channel的缓冲大小很简单就是channel可以存储多少元素。ch:= make(chan bool, 4)创建了可以存储4个元素的bool 型channel。在这个channel 中前4个元素可以无阻塞的写入。当写入第5个元素时代码将会阻塞直到其他goroutine从channel 中读取一些元素,腾出空间。 上面我们介绍了默认的非缓存类型的channel不过Go也允许指定channel的缓冲大小很简单就是channel可以存储多少元素。ch:= make(chan bool, 4)创建了可以存储4个元素的bool 型channel。在这个channel 中前4个元素可以无阻塞的写入。当写入第5个元素时代码将会阻塞直到其他goroutine从channel 中读取一些元素,腾出空间。
```Go ```Go
ch := make(chan type, value) ch := make(chan type, value)
``` ```
当 value = 0 时channel 是无缓冲阻塞读写的当value > 0 时channel 有缓冲、是非阻塞的,直到写满 value 个元素才阻塞写入。 当 value = 0 时channel 是无缓冲阻塞读写的当value > 0 时channel 有缓冲、是非阻塞的,直到写满 value 个元素才阻塞写入。
我们看一下下面这个例子你可以在自己本机测试一下修改相应的value值 我们看一下下面这个例子你可以在自己本机测试一下修改相应的value值
```Go ```Go
package main package main
import "fmt" import "fmt"
func main() { func main() {
c := make(chan int, 2)//修改2为1就报错修改2为3可以正常运行 c := make(chan int, 2)//修改2为1就报错修改2为3可以正常运行
c <- 1 c <- 1
c <- 2 c <- 2
fmt.Println(<-c) fmt.Println(<-c)
fmt.Println(<-c) fmt.Println(<-c)
} }
//修改为1报如下的错误: //修改为1报如下的错误:
//fatal error: all goroutines are asleep - deadlock! //fatal error: all goroutines are asleep - deadlock!
``` ```
@@ -124,28 +124,28 @@ channel通过操作符`<-`来接收和发送数据
上面这个例子中我们需要读取两次c这样不是很方便Go考虑到了这一点所以也可以通过range像操作slice或者map一样操作缓存类型的channel请看下面的例子 上面这个例子中我们需要读取两次c这样不是很方便Go考虑到了这一点所以也可以通过range像操作slice或者map一样操作缓存类型的channel请看下面的例子
```Go ```Go
package main package main
import ( import (
"fmt" "fmt"
) )
func fibonacci(n int, c chan int) { func fibonacci(n int, c chan int) {
x, y := 1, 1 x, y := 1, 1
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
c <- x c <- x
x, y = y, x + y x, y = y, x + y
}
close(c)
} }
close(c)
}
func main() { func main() {
c := make(chan int, 10) c := make(chan int, 10)
go fibonacci(cap(c), c) go fibonacci(cap(c), c)
for i := range c { for i := range c {
fmt.Println(i) fmt.Println(i)
}
} }
}
``` ```
`for i := range c`能够不断的读取channel里面的数据直到该channel被显式的关闭。上面代码我们看到可以显式的关闭channel生产者通过内置函数`close`关闭channel。关闭channel之后就无法再发送任何数据了在消费方可以通过语法`v, ok := <-ch`测试channel是否被关闭。如果ok返回false那么说明channel已经没有任何数据并且已经被关闭。 `for i := range c`能够不断的读取channel里面的数据直到该channel被显式的关闭。上面代码我们看到可以显式的关闭channel生产者通过内置函数`close`关闭channel。关闭channel之后就无法再发送任何数据了在消费方可以通过语法`v, ok := <-ch`测试channel是否被关闭。如果ok返回false那么说明channel已经没有任何数据并且已经被关闭。
@@ -159,66 +159,66 @@ channel通过操作符`<-`来接收和发送数据
`select`默认是阻塞的只有当监听的channel中有发送或接收可以进行时才会运行当多个channel都准备好的时候select是随机的选择一个执行的。 `select`默认是阻塞的只有当监听的channel中有发送或接收可以进行时才会运行当多个channel都准备好的时候select是随机的选择一个执行的。
```Go ```Go
package main package main
import "fmt" import "fmt"
func fibonacci(c, quit chan int) { func fibonacci(c, quit chan int) {
x, y := 1, 1 x, y := 1, 1
for { for {
select { select {
case c <- x: case c <- x:
x, y = y, x + y x, y = y, x + y
case <-quit: case <-quit:
fmt.Println("quit") fmt.Println("quit")
return return
}
} }
} }
}
func main() { func main() {
c := make(chan int) c := make(chan int)
quit := make(chan int) quit := make(chan int)
go func() { go func() {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
fmt.Println(<-c) fmt.Println(<-c)
} }
quit <- 0 quit <- 0
}() }()
fibonacci(c, quit) fibonacci(c, quit)
} }
``` ```
`select`里面还有default语法`select`其实就是类似switch的功能default就是当监听的channel都没有准备好的时候默认执行的select不再阻塞等待channel `select`里面还有default语法`select`其实就是类似switch的功能default就是当监听的channel都没有准备好的时候默认执行的select不再阻塞等待channel
```Go ```Go
select { select {
case i := <-c: case i := <-c:
// use i // use i
default: default:
// 当c阻塞的时候执行这里 // 当c阻塞的时候执行这里
} }
``` ```
## 超时 ## 超时
有时候会出现goroutine阻塞的情况那么我们如何避免整个程序进入阻塞的情况呢我们可以利用select来设置超时通过如下的方式实现 有时候会出现goroutine阻塞的情况那么我们如何避免整个程序进入阻塞的情况呢我们可以利用select来设置超时通过如下的方式实现
```Go ```Go
func main() { func main() {
c := make(chan int) c := make(chan int)
o := make(chan bool) o := make(chan bool)
go func() { go func() {
for { for {
select { select {
case v := <- c: case v := <- c:
println(v) println(v)
case <- time.After(5 * time.Second): case <- time.After(5 * time.Second):
println("timeout") println("timeout")
o <- true o <- true
break break
}
} }
}() }
<- o }()
} <- o
}
``` ```
## runtime goroutine ## runtime goroutine