golang多协程异步读写demo

一,利用两个sync.WaitGroup和通过关闭channel实现

package main

import (
	"fmt"
	"sync"
)

func main() {
	kk := make(chan int, 1000)
	var setwg sync.WaitGroup
	var readwg sync.WaitGroup
	for i := 1; i < 3; i++ {
		setwg.Add(1)
		go Set(kk, &setwg)
	}
	for i := 1; i < 3; i++ {
		readwg.Add(1)
		go Read(kk,&readwg)
	}
	setwg.Wait()


	close(kk)  //只要一个 chan 还有未读的数据,即使把它 close 掉,
	// 你还是可以继续把这些未读的数据消费完,而且bool值为true,之后才是读取零值数据和false
	readwg.Wait()

	//或者下面的实现也可以
	//for {
	//	if len(kk) == 0 {
	//		close(kk)
	//		readwg.Wait()
	//		break
	//	}
	//}
}
func Set(kk chan int, wg *sync.WaitGroup) {
	defer wg.Done()
	for i := 1; i < 3; i++ {
		kk <- i
	}
}

func Read(kk chan int,wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		i, ok := <-kk
		if !ok{
			fmt.Println("ok:",ok)
			break
		}
		fmt.Println(ok)
		fmt.Println(i)
	}
}



二,利用两个sync.WaitGroup和通过context的cancel操作实现

package main

import (
	"context"
	"fmt"
	"sync"
)

func main() {
	kk := make(chan int, 1000)
	ctx, cancel := context.WithCancel(context.Background())
	var setwg sync.WaitGroup
	var readwg sync.WaitGroup
	for i := 1; i < 3; i++ {
		setwg.Add(1)
		go Set(kk, &setwg)
	}
	for i := 1; i < 3; i++ {
		readwg.Add(1)
		go Read(kk, ctx,&readwg)
	}
	setwg.Wait()
	for {
		if len(kk) == 0 {
			cancel()
			readwg.Wait()
			break
		}
	}
}
func Set(kk chan int, wg *sync.WaitGroup) {
	defer wg.Done()
	for i := 1; i < 3; i++ {
		kk <- i
	}
}

func Read(kk chan int, ctx context.Context,wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		select {
		case <-ctx.Done():
			return
		default:
			if len(kk)>0{
				i:=<-kk
				fmt.Println(i)
			}
		}
	}
}

三,利用一个sync.WaitGroup和通过context的cancel操作及判断goruntine数量实现(意义不大)

package main

import (
	"context"
	"fmt"
	"runtime"
	"sync"
)

func main() {
	kk := make(chan int, 1000)
	ctx, cancel := context.WithCancel(context.Background())
	var setwg sync.WaitGroup
	for i := 1; i < 3; i++ {
		setwg.Add(1)
		go Set(kk, &setwg)
	}
	for i := 1; i < 3; i++ {
		go Read(kk, ctx)
	}
	setwg.Wait()
	for {
		if len(kk) == 0 {
			cancel()
			break
		}
	}
	for {
		fmt.Println("runtime.NumGoroutine()",runtime.NumGoroutine())
		if runtime.NumGoroutine()==1 {
			break
		}
	}
}
func Set(kk chan int, wg *sync.WaitGroup) {
	defer wg.Done()
	for i := 1; i < 3; i++ {
		kk <- i
	}
}

func Read(kk chan int, ctx context.Context) {
	for {
		select {
		case <-ctx.Done():
			return
		default:
			if len(kk)>0{
				i:=<-kk
				fmt.Println(i)
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值