介绍后期完善…
package main
import (
"fmt"
"sync"
"time"
)
func main() {
// syncMutex()
// syncRWMutex()
// syncMap()
syncPool()
}
func syncMutex() {
l := &sync.Mutex{}
fmt.Println("程序启动")
go lockMutexFunc(l)
go lockMutexFunc(l)
go lockMutexFunc(l)
go lockMutexFunc(l)
for {
}
}
func lockMutexFunc(lock *sync.Mutex) {
lock.Lock()
fmt.Println("疯狂刮痧----")
time.Sleep(1 * time.Second)
lock.Unlock()
}
func syncRWMutex() {
l := &sync.RWMutex{}
fmt.Println("程序启动")
go lockRWMutexFunc(l)
go lockRWMutexFunc(l)
go lockRWMutexFunc1(l)
go lockRWMutexFunc1(l)
go lockRWMutexFunc1(l)
go lockRWMutexFunc1(l)
for {
}
}
func lockRWMutexFunc(lock *sync.RWMutex) {
lock.Lock() // 写的时候排斥其他的写锁和读锁
fmt.Println("疯狂治疗----")
time.Sleep(1 * time.Second)
lock.Unlock()
}
func lockRWMutexFunc1(lock *sync.RWMutex) {
lock.RLock() // 读取的时候,不堵塞其他读锁,但是会排斥写锁
fmt.Println("疯狂刮痧----")
time.Sleep(1 * time.Second)
lock.RUnlock()
}
func syncMap() {
m := sync.Map{}
m.Store(1, 1)
m.Store(2, 2)
m.Store(3, 3)
m.Range(func(key, value interface{}) bool {
fmt.Println(key, value)
time.Sleep(1 * time.Second)
return true // false 终止
})
}
func syncPool() {
p := &sync.Pool{}
p.Put(1)
p.Put(2)
p.Put(3)
p.Put(4)
p.Put(5)
p.Put(6)
p.Put(7)
for i := 0; i < 9; i++ {
fmt.Println(p.Get())
}
}
正在学习Go语言的PHP程序员。