golang ---Learn Concurrency
https://github.com/golang/go/wiki/LearnConcurrency
实例1:
package main import (
"fmt"
"time"
) func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
} func main() {
go say("world")
say("hello")
}
输出:
world
hello
hello
world
world
hello
hello
world
hello
可以看到只输出了4个world就退出了,因为main执行完say("hello")就退出了
修改如下:
package main import (
"fmt"
"time"
) func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
} func main() {
go say("world")
var input string
fmt.Scanln(&input) //程序停止执行,等待用户输入
}
输出:
world
world
world
world
world
end //输入的string
world完整输出了
golang ---Learn Concurrency的更多相关文章
- Golang 并发concurrency
并发concurrency 很多人都是冲着Go大肆宣扬的高并发而忍不住跃跃欲试,但其实从源码解析来看,goroutine只是由官方实现的超级"线程池"而已.不过话说回来,每个实例4 ...
- Golang Learn Log #0
Print/Printf 区别 Print: 可以打印出字符串, 和变量 fmt.Println(var) //right fmt.Println("string") //righ ...
- Netty实现高性能IOT服务器(Groza)之精尽代码篇中
运行环境: JDK 8+ Maven 3.0+ Redis 技术栈: SpringBoot 2.0+ Redis (Lettuce客户端,RedisTemplate模板方法) Netty 4.1+ M ...
- Go语言的9大优势和3大缺点, GO语言最初的定位就是互联网时代的C语言, 我为什么放弃Go语言
Go语言的9大优势和3大缺点 转用一门新语言通常是一项大决策,尤其是当你的团队成员中只有一个使用过它时.今年 Stream 团队的主要编程语言从 Python 转向了 Go.本文解释了其背后的九大原因 ...
- goroutine 分析 协程的调度和执行顺序 并发写
package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...
- MIT 6.824学习笔记2 RPC/Thread
本节内容:Lect 2 RPC and Threads 线程:Threads allow one program to (logically) execute many things at onc ...
- goroutine 分析 协程的调度和执行顺序 并发写 run in the same address space 内存地址 闭包 存在两种并发 确定性 非确定性的 Go 的协程和通道理所当然的支持确定性的并发方式(
package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...
- Learn golang: Top 30 Go Tutorials for Programmers Of All Levels
https://stackify.com/learn-go-tutorials/ What is Go Programming Language? Go, developed by Google in ...
- golang语言中的context详解,Go Concurrency Patterns: Context
https://blog.golang.org/context Introduction In Go servers, each incoming request is handled in its ...
随机推荐
- 个人项目 python实现
一. github地址:https://github.com/zjh1234562/WC 二 . PSP表格 PSP2.1 Personal Software Process Stages 预估耗时 ...
- Linux shell 函数应用示例01
函数Function的使用 定义函数 (1) 函数名称() { ... ... } (2) function 函数名称{ ... ... } 调用函数 ...
- Python:日常应用汇总
判断路径中是否包含中文 import re def IsContainChinese(path:str) -> bool : cnPatter=re.compile(u'[\u4e00-\u9f ...
- 如何使用和关闭onbeforeunload 默认的浏览器弹窗事件
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来指定或者在<body>里指定.区别在于o ...
- 2-剑指offer: 最小的K个数
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 代码: // 这种topN问题比较常见的是使用堆来解决,最小的k个 ...
- selenium+python关于页面滚动条滑动到底的问题总结
1.如果滚动条是针对整个HTML可以用如下方式: js = "var q=document.documentElement.scrollTop=10000" # documentE ...
- vue2.0 实现导航守卫(路由守卫)---登录验证
路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...
- drf框架 - 视图家族 | GenericAPIView | mixins | generics | viewsets
视图家族 view:视图 generics:工具视图 mixins:视图工具集 viewsets:视图集 学习曲线: APIView => GenericAPIView => mixins ...
- ansible-playbook实例
准备前提 配置ansible主机详情:https://www.cnblogs.com/security-guard/ nginx的安装 编写nginx的自动部署文件nginx.yml hos ...
- Layui 表单赋值 编辑页面赋初值
原文:https://blog.csdn.net/yulongxue/article/details/97924591 //编辑 if (id > 0) { $.post("/Hand ...