Go语言有缓冲和无缓冲通道实现样例
感觉可以,但不好用。
应该有封装程序更高的包包吧。
package main
import (
"math/rand"
"fmt"
"time"
"sync"
)
const (
numberGoroutines = 4
taskLoad = 10
)
var (
wg sync.WaitGroup
wg2 sync.WaitGroup
wg3 sync.WaitGroup
)
func init() {
rand.Seed(time.Now().Unix())
}
func main() {
court := make(chan int)
wg.Add(2)
go player("Nadal", court)
go player("Djokovic", court)
court <- 1
wg.Wait()
baton := make(chan int)
wg2.Add(1)
go Runner(baton)
baton <- 1
wg2.Wait()
tasks := make(chan string, taskLoad)
wg3.Add(numberGoroutines)
for gr := 1; gr <= numberGoroutines; gr++ {
go worker(tasks, gr)
}
for post := 1; post <= taskLoad; post++ {
tasks <- fmt.Sprintf("Task: %d", post)
}
close(tasks)
wg3.Wait()
}
func player(name string, court chan int) {
defer wg.Done()
for {
ball, ok := <- court
if !ok {
fmt.Printf("Player %s Won\n", name)
return
}
n := rand.Intn(100)
if n % 13 == 0 {
fmt.Printf("Player %s Missed\n", name)
close(court)
return
}
fmt.Printf("Player %s Hit %d\n", name, ball)
ball++
court <- ball
}
}
func Runner(baton chan int) {
var newRunner int
runner := <-baton
fmt.Printf("Runner %d Running With Baton\n", runner)
if runner != 4 {
newRunner = runner + 1
fmt.Printf("Runner %d To The Line\n", newRunner)
go Runner(baton)
}
time.Sleep(100 * time.Millisecond)
if runner == 4 {
fmt.Printf("Runner %d Finished, Race Over\n", runner)
wg2.Done()
return
}
fmt.Printf("Runner %d Exchange With Runner %d\n",
runner,
newRunner)
baton <- newRunner
}
func worker(tasks chan string, worker int) {
defer wg3.Done()
for {
task, ok := <- tasks
if !ok {
fmt.Printf("Worker: %d: Shutting Down\n", worker)
return
}
fmt.Printf("Worker: %d: Started %s\n", worker, task)
sleep := rand.Int63n(100)
time.Sleep(time.Duration(sleep) * time.Millisecond)
fmt.Printf("Worker: %d: Completed %s\n", worker, task)
}
}

Go语言有缓冲和无缓冲通道实现样例的更多相关文章
- 标准I/O缓冲:全缓冲、行缓冲、无缓冲
说明:我仅仅对网络资源进行了整合,方便学习-.- 基于流的操作终于会调用read或者write函数进行I/O操作.为了使程序的执行效率最高,流对象一般会提供缓冲区,以降低调用系统I/O库函数的次数. ...
- unix 全缓冲、行缓冲、无缓冲
基于流的操作最终会调用read或者write函数进行I/O操作.为了使程序的运行效率最高,流对象通常会提供缓冲区,以减少调用系统I/O库函数的次数. 基于流的I/O提供以下3种缓冲: 全 缓冲:直到缓 ...
- go语言之进阶篇无缓冲channel
1.无缓冲channel 示例: package main import ( "fmt" "time" ) func main() { //创建一个无缓存的ch ...
- 2018上C语言程序设计(高级)博客作业样例
要求一(20分) 完成PTA中题目集名为<usth-C语言高级-第1次作业>中的所有题目. 要求二 PTA作业的总结(20分+30分) 将PTA第1次作业作业中以下2道题的解题思路按照规定 ...
- C语言 一维数组叠加为二维数组样例
这里参看memcpy的用法,将一个一维整型数组不停的叠加为二维数组 使用宏定义来控制二维数组的行列 代码如下: #include <stdio.h> #include <stdlib ...
- Go语言的通道(1)-无缓冲通道
前言: 上文中我们采用了[原子函数]已经[共享锁]两种方式分别对多个goroutine进行了同步,但是在go语言中提供了另一种更好的方式,那就是使用通道(Channel). 一.通道是什么? 其实无论 ...
- Golang并发编程有缓冲通道和无缓冲通道(channel)
无缓冲通道 是指在接收前没有能力保存任何值得通道.这种类型的通道要求发送goroutine和接收goroutine同时准备好,才能完成发送和接收操作.如果两个goroutine没有同时准备好,通道会导 ...
- golang的缓冲channel和无缓冲channel的区别
话说golang的channel同步的定义真是让人无力吐槽,码农的用户体验就这么难搞么,超耐磨阿,无缓冲和缓冲居然有这么大区别....靠 转载一段网上的资料 --------------------- ...
- go之无缓冲channel(通道)和有缓冲channel(通道)
channel我们先来看一下通道的解释:channel是Go语言中的一个核心类型,可以把它看成管道.并发核心单元通过它就可以发送或者接收数据进行通讯,这在一定程度上又进一步降低了编程的难度.chann ...
随机推荐
- JavaScript对iframe的DOM操作
在IE6.IE7中,我们可以使用 document.frames[ID].document 来访问iframe子窗口中的document对象,可是这是不符合W3C标准的写法,也是IE下独有的方法,在F ...
- HDU 5656
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- Android 解决adb server is out of date. killing... ADB server didn't ACK * failed to star
The connection to adb is down, and a severe error has occured. [-- :: - HelloOPone] You must restart ...
- G. Trace ACM-ICPC 2018 徐州赛区网络预赛 线段树写法
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ...
- c# MD5盐值加密
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...
- idea中设置springboot热部署
在idea中设置springboot热部署,项目修改的时候不用手动重启应用 1,pom中添加依赖 <dependency> <groupId>org.springframewo ...
- asyncio结合线程池
#使用多线程:在协程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor import socket fro ...
- Windows、Linux及Mac查看端口和杀死进程
本文介绍如何在Windows.Linux及Mac下查看端口和杀死进程. Windows下查看端口和杀死进程 查看占用端口号的进程号:netstat –ano | findstr "指定端口号 ...
- 【51NOD-0】1046 A^B Mod C
[算法]快速幂运算 [题解]快速幂的原理是把幂用二进制表示,从最低位a,次低位a2,次次低位(a2)2. #include<cstdio> long long quick_pow(long ...
- Java 对象排序详解
很难想象有Java开发人员不曾使用过Collection框架.在Collection框架中,主要使用的类是来自List接口中的ArrayList,以及来自Set接口的HashSet.TreeSet,我 ...