码迷,mamicode.com
首页 > 其他好文 > 详细

channle阻塞

时间:2018-12-07 00:33:16      阅读:16      评论:0      收藏:0      [点我收藏+]

标签:port   sleep   bool   package   read   imp   func   pac   second   

 

 

package main

import (
	"fmt"
	"time"
)

func readChan(inchan chan int, exitchan chan bool) {
	for {
		time.Sleep(time.Millisecond * 500)
		res, ok := <-inchan
		if !ok {
			break
		}
		fmt.Printf("读取%d\n", res)
	}
	exitchan <- true
	close(exitchan)
}

func writeChan(inchan chan int) {
	for i := 1; i <= 50; i++ {
		inchan <- i
		fmt.Printf("写入%d\n", i)
	}
	close(inchan)
}
func main() {
	inchan := make(chan int, 10)
	exitchan := make(chan bool, 1)

	go writeChan(inchan)
	go readChan(inchan, exitchan)

	for {
		_, ok := <-exitchan
		if !ok {
			break
		}
	}
}

  

channle阻塞

标签:port   sleep   bool   package   read   imp   func   pac   second   

原文地址:https://www.cnblogs.com/LC161616/p/10080596.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!