Compare commits
2 Commits
bdd898be69
...
684b0a09b8
Author | SHA1 | Date | |
---|---|---|---|
684b0a09b8 | |||
e98d57e6d1 |
37
README.md
37
README.md
@ -1,3 +1,38 @@
|
||||
# go-concurrency
|
||||
|
||||
Aims to learn the knowledge about concurrency in Go
|
||||
Aims to learn the knowledge about concurrency in Go
|
||||
|
||||
## Flow control with channels
|
||||
|
||||
|
||||
1. We can imagine that channel is a pipe, `chan<-` means to send a value into the channel. `<-chan` means to receive a value from the channel.
|
||||
|
||||
2. `select ... case` can execute sending value into a channel
|
||||
|
||||
|
||||
## Range and Close
|
||||
|
||||
1. A sender can close a channel to indicate that no more values will be sent.
|
||||
2. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression. `v, ok := <-ch`, `ok` is `false` if there are no more values to receive and the channel is closed.
|
||||
3. The loop for `i:= range c` receives values from the channel repeatedly until it is closed.
|
||||
4. Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.
|
||||
5. Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a `range` loop.
|
||||
|
||||
|
||||
## Select
|
||||
|
||||
1. The `select` statement lets a goroutine wait on multiple communication operations
|
||||
2. A `select` blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready
|
||||
|
||||
## Default selection
|
||||
|
||||
1. The `default` case in a `select` is run if no other case is ready.
|
||||
2. Use a `default` case to try a send or receive without blocking
|
||||
```
|
||||
select {
|
||||
case i:= <-c:
|
||||
// use i
|
||||
default:
|
||||
// receive when c is blocked
|
||||
}
|
||||
```
|
25
basics/default-select/main.go
Normal file
25
basics/default-select/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tick := time.Tick(100 * time.Millisecond)
|
||||
|
||||
boom := time.After(5000 * time.Millisecond)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tick:
|
||||
log.Println("tick.")
|
||||
case <-boom:
|
||||
log.Println("boom")
|
||||
return
|
||||
default:
|
||||
log.Println(" .")
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
25
basics/range-close/main.go
Normal file
25
basics/range-close/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func fibonacci(n int, ch chan int) {
|
||||
x, y := 0, 1
|
||||
for i := 0; i < n; i++ {
|
||||
ch <- x
|
||||
x, y = y, x+y
|
||||
}
|
||||
close(ch)
|
||||
}
|
||||
|
||||
func main() {
|
||||
ch := make(chan int, 10)
|
||||
|
||||
go fibonacci(cap(ch), ch)
|
||||
for i := range ch {
|
||||
log.Println(i)
|
||||
}
|
||||
|
||||
log.Println("Done")
|
||||
}
|
32
basics/select/main.go
Normal file
32
basics/select/main.go
Normal file
@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
func fibonacci(ch, quit chan int) {
|
||||
x, y := 0, 1
|
||||
for {
|
||||
select {
|
||||
case ch <- x:
|
||||
x, y = y, x+y
|
||||
case <-quit:
|
||||
log.Println("quit")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ch := make(chan int, 10)
|
||||
|
||||
quit := make(chan int)
|
||||
|
||||
go func() {
|
||||
for i := 0; i < cap(ch); i++ {
|
||||
log.Println(<-ch)
|
||||
|
||||
}
|
||||
quit <- 0
|
||||
}()
|
||||
|
||||
fibonacci(ch, quit)
|
||||
}
|
Loading…
Reference in New Issue
Block a user