select
- select 使用
- 定时器使用
- 在select中使用Nil Channel
package main
import (
"fmt"
"time"
"math/rand"
)
func generator() chan int {
out := make(chan int)
go func() {
i := 0
for {
time.Sleep(
time.Duration(rand.Intn(1500)) *
time.Millisecond)
out <- i
i++
}
}()
return out
}
func worker(id int, c chan int) {
for n := range c {
time.Sleep(time.Second)
fmt.Printf("Worker %d receieved %d\n",
id, n)
}
}
func createWorker(id int) chan<- int {
c := make(chan int)
go worker(id, c)
return c
}
func main() {
var c1, c2 = generator(),generator()
var woker = createWorker(0)
var values []int
tm := time.After(10 * time.Second)
tick := time.Tick(time.Second)
for {
var activeValue int
var activeWorker chan<- int
if len(values) > 0 {
activeWorker = woker
activeValue = values[0]
}
select {
case n := <- c1:
values = append(values, n)
case n := <- c2:
values = append(values, n)
case activeWorker <- activeValue:
values = values[1:]
case <-time.After(800 * time.Millisecond):
fmt.Println("Timeout")
case <-tm:
fmt.Println("bye...")
return
case <- tick:
fmt.Printf("queue len = %d\n", len(values))
}
}
}
传统的同步机制
package main
import (
"fmt"
"time"
"sync"
)
type atomicInt struct {
value int
lock sync.Mutex
}
func (a *atomicInt) increment() {
fmt.Println("safe increment")
func() {
a.lock.Lock()
defer a.lock.Unlock()
a.value++
}()
}
func (a *atomicInt) get() int {
a.lock.Lock()
defer a.lock.Unlock()
return a.value
}
func main() {
var a atomicInt
a.increment()
go func() {
a.increment()
}()
time.Sleep(time.Millisecond)
fmt.Println(a.get())
}