当前位置: 首页 > news >正文

go的sync包学习

包含了sync.Mutex,sync.RWMutex,sync.Cond,sync.Map,sync.Once等demo

sync.Mutex


//讲解mutex
import ("fmt""math/rand""sync""time"
)type Toilet struct {m sync.Mutex
}
type Person struct {Name string
}var DateTime = "2006-01-02 15:04:05"var otherThing = []string{"唱歌", "跳舞", "修仙"}func main() {t := &Toilet{}wg := sync.WaitGroup{}wg.Add(5)for i := 0; i < 5; i++ {go func(i int, t *Toilet, wg *sync.WaitGroup) {p := Person{Name: fmt.Sprintf("%d 号", i),}p.InToiletLock(t, wg)}(i, t, &wg)}wg.Wait()}func (p *Person) InToiletLock(t *Toilet, wg *sync.WaitGroup) {defer wg.Done()t.m.Lock()i := rand.Intn(5)fmt.Printf("%s 上厕所时间 %v\n", p.Name, time.Now().Format(DateTime))time.Sleep(time.Duration(i) * time.Second)t.m.Unlock()
}
func (p *Person) InToiletUnLock(wg *sync.WaitGroup) {defer wg.Done()i := rand.Intn(5)fmt.Printf("%s 上厕所时间 %v\n", p.Name, time.Now().Format(DateTime))time.Sleep(time.Duration(i) * time.Second)
}

在这里插入图片描述

sync.RWMutex

package mainimport ("fmt""math/rand""sync""time"
)type Cinema struct {rw *sync.RWMutex
}type Person struct {Name string
}type Admin struct {Name string
}func main() {//example01()exmples2()
}func (p *Person) SitDown(wg *sync.WaitGroup) {defer wg.Done()fmt.Printf("%s 观众坐下!\n", p.Name)
}func (p *Person) StartWatch() {fmt.Printf("%s 开始观看!\n", p.Name)i := time.Duration(rand.Intn(5) + 1)time.Sleep(i * time.Second)fmt.Printf("%s 观看结束!\n", p.Name)
}func (p *Person) WatchMovie(c *Cinema, wg *sync.WaitGroup, b chan int) {c.rw.RLock()//先坐下p.SitDown(wg)<-b//看电影p.StartWatch()c.rw.RUnlock()
}func (a *Admin) ChangeMovie(c *Cinema, wg *sync.WaitGroup) {defer wg.Done()c.rw.Lock()fmt.Printf("所有观众看完电影,%s管理员切换影片\n", a.Name)c.rw.Unlock()
}
func example01() {c := &Cinema{rw: &sync.RWMutex{}}a := &Admin{Name: "超人"}b := make(chan int)wg := sync.WaitGroup{}wg.Add(10)for i := 0; i < 10; i++ {go func(i int, c *Cinema, wg *sync.WaitGroup, b chan int) {p := Person{Name: fmt.Sprintf("%d 号观众", i),}p.WatchMovie(c, wg, b)}(i, c, &wg, b)}wg.Wait()fmt.Printf("所有观众入座,开始播放电影\n")close(b)wg.Add(1)go func(wg *sync.WaitGroup) {a.ChangeMovie(c, wg)}(&wg)wg.Wait()
}type Desk struct {rw     *sync.RWMutexSnacks int
}func (d *Desk) PlaceSnacks(wg *sync.WaitGroup) {defer wg.Done()d.rw.Lock()d.Snacks = 10time.Sleep(3 * time.Second)fmt.Printf("零食准备完毕...\n")d.rw.Unlock()
}
func (p *Person) GetSnacks(d *Desk, wg *sync.WaitGroup) {for !d.rw.TryRLock() {fmt.Printf("桌上没零食,%s 望眼欲穿\n", p.Name)time.Sleep(2 * time.Second)}d.rw.RUnlock()defer wg.Done()defer d.rw.Unlock()d.rw.Lock()if d.Snacks > 0 {fmt.Printf("%s 抢到零食,开心\n", p.Name)d.Snacks--return}fmt.Printf("%s 没抢到零食,难受\n", p.Name)
}
func exmples2() {d := Desk{rw: &sync.RWMutex{},}wg := sync.WaitGroup{}wg.Add(12)go d.PlaceSnacks(&wg)for i := 0; i < 11; i++ {p := Person{Name: fmt.Sprintf("%d 号猿猴", i),}go p.GetSnacks(&d, &wg)}wg.Wait()
}

sync.Cond

package main//sync.Cond进行学习import ("fmt""sync""time"
)var start = false
var done = falsefunc main() {wg := sync.WaitGroup{}wg.Add(10)m := &sync.Mutex{}c := sync.NewCond(m)for i := 0; i < 10; i++ {go Soliders(i, c, &wg)}go Waiter(c)Officer(c)wg.Wait()fmt.Println("所有大兵干完饭")CleanUp(c)time.Sleep(time.Second * 3)fmt.Println("打扫结束")
}func CleanUp(c *sync.Cond) {c.L.Lock()done = truec.L.Unlock()c.Signal()}
func Officer(c *sync.Cond) {fmt.Printf("长官准备中....\n")time.Sleep(time.Second * 5)c.L.Lock()start = truec.L.Unlock()c.Broadcast()
}func Soliders(i int, c *sync.Cond, wg *sync.WaitGroup) {defer wg.Done()c.L.Lock()fmt.Printf("大兵%d号等待干饭...\n", i)for !start {c.Wait()}fmt.Printf("大兵%d号开始干饭...\n", i)c.L.Unlock()
}func Waiter(c *sync.Cond) {c.L.Lock()for !done {c.Wait()}fmt.Println("用餐结束,开始打扫......")c.L.Unlock()return
}

sync.Map


func main() {m := make(map[int]int)for i := 0; i < 1000; i++ {go func(m map[int]int) {m[i] = i}(m)}
}func TestStore(t *testing.T) {var sum sync.Mapsum.Store("a", 1)//sum.Delete("a")val, ok := sum.Load("a")fmt.Println(val, ok)
}func TestLoadAndDelete(t *testing.T) {var smm sync.Mapsmm.Store("a", 1)val, loaded := smm.LoadAndDelete("a")fmt.Printf("value is %v,is exist %v \n", val, loaded)_, ok := smm.LoadAndDelete("a")fmt.Printf("value is ,is exist %v \n", ok)
}func TestLoadAndStore(t *testing.T) {var smm sync.Mapvalue, loaded := smm.LoadOrStore("a", 1)fmt.Printf("value is %v,is exist %v \n", value, loaded)value, loaded = smm.LoadOrStore("a", 2)fmt.Printf("value is %v,is exist %v \n", value, loaded)smm.Store("a", 3)value, loaded = smm.Load("a")fmt.Printf("value is %v,is exist %v \n", value, loaded)
}func TestRange(t *testing.T) {var smm sync.Mapfor i := 0; i < 10; i++ {go func(m *sync.Map, key int, value int) {m.Store(key, value)}(&smm, i, i)}time.Sleep(2 * time.Second)smm.Range(func(key, value interface{}) bool {fmt.Println(key, value)return true})
}

sync.pool

import ("bytes""log""net/http""runtime""sync""time"
)var pool = sync.Pool{New: func() interface{} { return new([]bytes.Buffer) }}func main() {var pool sync.Poolfor i := 0; i < 10; i++ {pool.Put(&http.Client{Timeout: time.Second * time.Duration(10),})}runtime.GC()runtime.GC()p := pool.Get()if p == nil {log.Fatal("p is nil")} else {pool.Put(p)}
}

sync.Once

package mainimport ("fmt""math/rand""sync""sync/atomic""testing""time"
)//主要涉及sync.Oncetype Config struct {Server stringPort   int64
}var (config       *Configonce         OncePowerinitedConfig uint32
)type OncePower struct {sync.Mutexdone uint32
}func init() {rand.Seed(time.Now().UnixNano())
}
func (p *OncePower) Do(fun func() error) error {if atomic.LoadUint32(&p.done) == 0 {return p.doSlow(fun)}return nil
}func (o *OncePower) doSlow(fun func() error) error {o.Lock()defer o.Unlock()if o.done == 0 {err := fun()if err != nil {return err}atomic.StoreUint32(&o.done, 1)}return nil
}
func initConfig() error {//randInt := rand.Intn(6)//if randInt > 2 {//	return errors.New("初始化失败了....")//}config = &Config{Server: "127.0.0.1",Port:   9999,}fmt.Println("init config....")if atomic.LoadUint32(&initedConfig) == 0 {atomic.StoreUint32(&initedConfig, 1)}return nil
}func ReadConfig(i int) *Config {fmt.Println("i >>>", i)err := once.Do(initConfig)fmt.Println("once err >>>", err)return config
}func TestSingleTon(t *testing.T) {wait := sync.WaitGroup{}for i := 0; i < 10; i++ {wait.Add(1)go func(i int) {defer wait.Done()_ = ReadConfig(i)}(i)}wait.Wait()
}func TestOnce(t *testing.T) {var once sync.OnceonceBody := func() {fmt.Println("only once")}done := make(chan bool)for i := 0; i < 10; i++ {go func() {once.Do(onceBody)done <- truefmt.Println("true done", i)}()}for i := 0; i < 10; i++ {<-done}
}
http://www.lryc.cn/news/532416.html

相关文章:

  • 互联网上常见的,ip地址泛播什么意思
  • Linux/C高级(精讲)----shell结构语句、shell数组
  • 14.kafka开机自启动配置
  • 11 享元(Flyweight)模式
  • PHP JSON操作指南
  • 【学习笔记】计算机图形学的几何数学基础知识
  • Python因为网络原因安装依赖库报错
  • 什么是卸荷器?风力发电为什么要用卸荷器
  • SQL Server详细使用教程(包含启动SQL server服务、建立数据库、建表的详细操作) 非常适合初学者
  • 大数据学习之Spark分布式计算框架RDD、内核进阶
  • Unity 加载OSGB(webgl直接加载,无需转换格式!)
  • tcp/ip网络协议,tcp/ip网络协议栈
  • 【Debug】the remote host closed the connection错误信息分析
  • SpringBoot扩展篇:@Scope和@Lazy源码解析
  • “AI隐患识别系统,安全多了道“智能护盾”
  • 通向AGI之路:人工通用智能的技术演进与人类未来
  • 论文阅读:InstanceDiffusion: Instance-level Control for Image Generation
  • 7.攻防世界 wzsc_文件上传
  • 以为是响应式对象丢失导致数据没有回显
  • 来 Gitcode 免费体验 DeepSeek 蒸馏模型,开启 AI 探索新旅程
  • 2.Mkdocs配置说明(mkdocs.yml)【最新版】
  • 云轴科技ZStack+海光DCU:率先推出DeepSeek私有化部署方案
  • 扩增子分析|零模型2——基于βNTI的微生物随机性和确定性装配过程(箱线图和柱状图R中实现)
  • 专题:剑指offer
  • DeepSeek 部署过程中的问题
  • DeepSeek R1本地化部署 Ollama + Chatbox 打造最强 AI 工具
  • 应急场景中的数据融合与对齐
  • 手机上运行AI大模型(Deepseek等)
  • Mellanox网卡信息查看
  • 【漫画机器学习】083.安斯库姆四重奏(Anscombe‘s quartet)