go routine channel 协程

/**
* @Author: wsp
* @Date: 2018/1/18 12:26
* @Description:
*/
package routinStudy

import (
"fmt"
"math/rand"
"runtime"
"time"
)

/**
* @Author: wsp
* @Date: 13:53 2018/1/18
* @Description: 简单启动一个协程,注意:主程序执行完成不会等待其他协程是否执行完毕
*/
func RoutinStudy1() {

go routinTest1("协程1")
go routinTest1("协程2")

// 注意:主程序执行完成不会等待其他协程是否执行完毕
fmt.Println("我是主程序")
time.Sleep(5 * time.Second)
}

func routinTest1(param string) {

for i := 0; i < 10; i++ {
fmt.Println(param, "是第", i, "次输出")
time.Sleep(time.Second)
}
}

/**
* @Author: wsp
* @Date: 13:53 2018/1/18
* @Description: 启动一个协程,主线程会等待chan中有数据才会继续执行,要不处于阻塞状态
* 注意:channel 当channel中没有数据时,读channel会出现阻塞,当channel中有数据时,写channel会出现阻塞
*/
func RoutinStudy3() {
var c chan int = make(chan int)
go testRoutin1(c)

result := <-c
fmt.Println("result:", result)
fmt.Println("程序结束!")
}

func testRoutin1(c chan int) {
i := 0
for {
i++
fmt.Println("我是第", i, "输出!")
if i == 10 {
c <- i
}
time.Sleep(time.Second * 1)
}
}

/**
* @Author: wsp
* @Date: 13:56 2018/1/18
* @Description: 两个协程之间相互通信
*/
func RoutinStudy2() {

fmt.Println(runtime.NumCPU())
// 协程间通信通过channel
var c chan int = make(chan int)
go sell(c)
go produce(c)
fmt.Println("我是主程序")
time.Sleep(10 * time.Second)
}

func sell(c chan int) {
for {
num := <-c
fmt.Println("Sell ", num, " bread")
}
}

func produce(c chan int) {
for {
num := rand.Intn(3)
t := time.Duration(num)
fmt.Println("Product ", num, " bread")
c <- num
time.Sleep(time.Second * t)

}
}

/**
* @Author: wsp
* @Date: 14:57 2018/1/18
* @Description: channel 当channel中没有数据时,读channel会出现阻塞,当channel中有数据时,写channel会出现阻塞
*/
func RoutinStudy4() {
var c chan int = make(chan int)
go channelRead(c)
go channelWrite(c)
time.Sleep(time.Second * 3)
}

func channelRead(c chan int) {
for {
time.Sleep(time.Second * 2)
//fmt.Println("等待读取数据...")
result := <-c
fmt.Println("读取数据为:", result)
}
}

func channelWrite(c chan int) {
var param int
for {
//time.Sleep(time.Second * 2)
fmt.Println("等待写入数据...")
param++
c <- param
//fmt.Println("写入数据:", param)
}
}

/**
* @Author: wsp
* @Date: 14:57 2018/1/18
* @Description: 带缓冲的channel
*/
func RoutinStudy5() {
var c chan int = make(chan int, 10)
go channelRead5(c)
go channelWrite5(c)
time.Sleep(time.Second * 3)
}

func channelRead5(c chan int) {
for {
time.Sleep(time.Second * 1)
//fmt.Println("等待读取数据...")
//result := <-c
for result := range c {
fmt.Println("读取数据为:", result)
}
}
}

func channelWrite5(c chan int) {
var param int
for {
time.Sleep(time.Millisecond * 100)
param++
fmt.Println("写入数据:", param)
c <- param
//fmt.Println("写入数据:", param)
}
}

/**
* @Author: wsp
* @Date: 15:34 2018/1/18
* @Description: 协程超时处理
*/
func RoutinStudy6() {
var c1 chan int = make(chan int)
var c2 chan int = make(chan int)
var timeover chan int = make(chan int)
go overtime(timeover)

select {
case x := <-c1:
fmt.Println("c1:", x)
case x := <-c2:
fmt.Println("c2:", x)
case x := <-timeover:
fmt.Println("timeover:", x)
}
fmt.Println("等待")
time.Sleep(time.Second * 5)
fmt.Println("主程序over")
}

func overtime(c chan int) {

time.Sleep(time.Second)
c <- 1
}
posted @ 2018-01-18 15:35  穷开心y  阅读(234)  评论(0编辑  收藏  举报