go 并发 demo
两个进程执行两个goroutine
// This sample program demonstrates how to create goroutines and
// how the scheduler behaves.
package main import (
"fmt"
"runtime"
"sync"
) // main is the entry point for all Go programs.
func main() {
// Allocate 1 logical processor for the scheduler to use.
runtime.GOMAXPROCS() // wg is used to wait for the program to finish.
// Add a count of two, one for each goroutine.
var wg sync.WaitGroup
wg.Add() fmt.Println("Start Goroutines") // Declare an anonymous function and create a goroutine.
go func() {
// Schedule the call to Done to tell main we are done.
defer wg.Done() // Display the alphabet three times
for count := ; count < ; count++ {
for char := 'a'; char < 'a'+; char++ {
fmt.Printf("%c ", char) }
fmt.Println()
}
}() // Declare an anonymous function and create a goroutine.
go func() {
// Schedule the call to Done to tell main we are done.
defer wg.Done() // Display the alphabet three times
for count := ; count < ; count++ {
for char := 'A'; char < 'A'+; char++ {
fmt.Printf("%c ", char)
}
fmt.Println()
}
}() // Wait for the goroutines to finish.
fmt.Println("Waiting To Finish")
wg.Wait() fmt.Println("\nTerminating Program")
}
输出
Start Goroutines
Waiting To Finish
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o X Y Z
A B C D p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
E F G H I J K L M N O P Q R S T U V W X Y Z
一个进程执行两个goroutine
// This sample program demonstrates how to create goroutines and
// how the scheduler behaves.
package main import (
"fmt"
"runtime"
"sync"
) // main is the entry point for all Go programs.
func main() {
// Allocate 1 logical processor for the scheduler to use.
runtime.GOMAXPROCS() // wg is used to wait for the program to finish.
// Add a count of two, one for each goroutine.
var wg sync.WaitGroup
wg.Add() fmt.Println("Start Goroutines") // Declare an anonymous function and create a goroutine.
go func() {
// Schedule the call to Done to tell main we are done.
defer wg.Done() // Display the alphabet three times
for count := ; count < ; count++ {
for char := 'a'; char < 'a'+; char++ {
fmt.Printf("%c ", char) }
fmt.Println()
}
}() // Declare an anonymous function and create a goroutine.
go func() {
// Schedule the call to Done to tell main we are done.
defer wg.Done() // Display the alphabet three times
for count := ; count < ; count++ {
for char := 'A'; char < 'A'+; char++ {
fmt.Printf("%c ", char)
}
fmt.Println()
}
}() // Wait for the goroutines to finish.
fmt.Println("Waiting To Finish")
wg.Wait() fmt.Println("\nTerminating Program")
}
输出
Start Goroutines
Waiting To Finish
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
go 并发 demo的更多相关文章
- golang 并发demo 写入 redis
原文链接:golang 并发demo 写入 redis 源代码: package main import ( "fmt" "runtime" "str ...
- python 多进程并发demo
outline 下午需要简单处理一份数据,就直接随手写脚本处理了,但发现效率太低,速度太慢,就改成多进程了: 程序涉及计算.文件读写,鉴于计算内容挺多的,就用多进程了(计算密集). 代码 import ...
- Disruptor并发框架(一)简介&上手demo
框架简介 Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一 ...
- disruptor 高并发编程 简介demo
原文地址:http://www.cnblogs.com/qiaoyihang/p/6479994.html disruptor适用于大规模低延迟的并发场景.可用于读写操作分离.数据缓存,速度匹配(因为 ...
- Java并发编程工具类 CountDownLatch CyclicBarrier Semaphore使用Demo
Java并发编程工具类 CountDownLatch CyclicBarrier Semaphore使用Demo CountDownLatch countDownLatch这个类使一个线程等待其他线程 ...
- 一个demo让你彻底理解Android触摸事件的并发
注:本文涉及的demo的地址:https://github.com/absfree/TouchDispatch 1. 触摸动作及事件序列 (1)触摸事件的动作 触摸动作一共有三种:ACTION_DOW ...
- java并发--流量控制demo
实现一个流控程序.控制客户端每秒调用某个远程服务不超过N次,客户端是会多线程并发调用,需要一个轻量简洁的实现,大家看看下面的一个实现,然后可以自己写一个实现. import java.util.Dat ...
- 并发编程系列小结(线程安全,synchronized,脏读,线程间的通信wait/notify,线程的三种实现方式Demo,可替代wait/notify的方法)
线程安全: 当多个线程访问某一个类(对象或方法)时,这个类始终都能表现出正确的行为,那么这个类(对象或方法就是线程安全的) synchronized: 可以在任意对象或方法上加锁,而加锁的这段代码称为 ...
- coding++:高并发解决方案限流技术---漏桶算法限流--demo
1.漏桶算法 漏桶作为计量工具(The Leaky Bucket Algorithm as a Meter)时,可以用于流量整形(Traffic Shaping)和流量控制(TrafficPolici ...
随机推荐
- 0627-File-163
print('创建一个文件向其中写入信息,再读取出写入的信息-------------------------------------') f = open('E:\\foo4.txt','w+') ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- testng入门教程13同文件数据驱动
下面是@DataProvider有name和没有name时 有name的时候可以引用name 即:@DataProvider(name="testData")----------& ...
- 登陆跳板机每天只输入一次token的方法——ssh clone session
自从跳板机升级后,无所不在的token让小PE很是恼火,于是有了这篇文章@_@ Linux or Mac篇 在Fedora或者Mac下很简单,修改~/.ssh/config文件,没有的话,就新建一个( ...
- python的subprocess的简单使用和注意事项
subprocess是python在2.4引入的模块, 主要用来替代下面几个模块和方法: os.systemos.spawn*os.popen*popen2.*commands.* 可以参考PEP32 ...
- HTML5 manifest离线缓存技术
干什么用的? 离线缓存为的是第一次请求后,根据manifest文件进行本地缓存,并且在下一次请求后进行展示(若有缓存的话,无需再次进行请求而是直接调用缓存),最根本的感觉是它使得WEB从online可 ...
- java多线程----JUC集合”01之 框架
java集合的架构.主体内容包括Collection集合和Map类:而Collection集合又可以划分为List(队列)和Set(集合). 1. List的实现类主要有: LinkedList, A ...
- clock_gettime接口和linux时间系统
RTC/OSCRTC,Real Time Clock,真实时钟,也叫做CMOS时钟,又叫walltimeOSC,Operation System Clock,系统时钟.在开机时操作系统取得RTC中的时 ...
- SaltStack安装及配置
1.简介SaltStack是一个服务器基础架构集中化管理平台,具备配置管理.远程执行.监控等功能,一般可以理解为简化版的puppet和加强版的func.SaltStack基于Python语言实现,结合 ...
- 性能优化之MySQL调优篇
MySQL对于很多Linux从业者而言,是一个非常棘手的问题,多数情况都是因为对数据库出现问题的情况和处理思路不清晰.在进行MySQL的优化之前必须要了解的就是MySQL的查询过程,很多的查询优化工作 ...