01_创建goroutine.go
package main
import (
"fmt"
"time"
)
func newTask() {
for {
fmt.Println("this is a newTask")
time.Sleep(time.Second) //延时1s
}
}
func main() {
go newTask() //新建一个协程, 新建一个任务
for {
fmt.Println("this is a main goroutine")
time.Sleep(time.Second) //延时1s
}
}
02_主goroutine先退出.go
package main
import (
"fmt"
"time"
)
//主协程退出了,其它子协程也要跟着退出
func main() {
go func() {
i := 0
for {
i++
fmt.Println("子协程 i = ", i)
time.Sleep(time.Second)
}
}() //别忘了()
i := 0
for {
i++
fmt.Println("main i = ", i)
time.Sleep(time.Second)
if i == 2 {
break
}
}
}
03_主协程先退出导致子协程没有来得及调用.go
package main
import (
"fmt"
"time"
)
//主协程退出了,其它子协程也要跟着退出
func main() {
go func() {
i := 0
for {
i++
fmt.Println("子协程 i = ", i)
time.Sleep(time.Second)
}
}() //别忘了()
}
04_Gosched的使用.go
package main
import (
"fmt"
"runtime"
)
func main() {
go func() {
for i := 0; i < 5; i++ {
fmt.Println("go")
}
}()
for i := 0; i < 2; i++ {
//让出时间片,先让别的协议执行,它执行完,再回来执行此协程
runtime.Gosched()
fmt.Println("hello")
}
}
05_Goexit的使用.go
package main
import (
"fmt"
"runtime"
)
func test() {
defer fmt.Println("ccccccccccccc")
//return //终止此函数
runtime.Goexit() //终止所在的协程
fmt.Println("dddddddddddddddddddddd")
}
func main() {
//创建新建的协程
go func() {
fmt.Println("aaaaaaaaaaaaaaaaaa")
//调用了别的函数
test()
fmt.Println("bbbbbbbbbbbbbbbbbbb")
}() //别忘了()
//特地写一个死循环,目的不让主协程结束
for {
}
}
06_GOMAXPROCS的使用.go
package main
import (
"fmt"
"runtime"
)
func main() {
//n := runtime.GOMAXPROCS(1) //指定以1核运算
n := runtime.GOMAXPROCS(4) //指定以4核运算
fmt.Println("n = ", n)
for {
go fmt.Print(1)
fmt.Print(0)
}
}
07_多任务资源竞争问题.go
package main
import (
"fmt"
"time"
)
//定义一个打印机,参数为字符串,按每个字符打印
//打印机属于公共资源
func Printer(str string) {
for _, data := range str {
fmt.Printf("%c", data)
time.Sleep(time.Second)
}
fmt.Printf("\n")
}
func person1() {
Printer("hello")
}
func person2() {
Printer("world")
}
func main() {
//新建2个协程,代表2个人,2个人同时使用打印机
go person1()
go person2()
//特地不让主协程结束,死循环
for {
}
}
08_通过channel实现同步.go
package main
import (
"fmt"
"time"
)
//全局变量,创建一个channel
var ch = make(chan int)
//定义一个打印机,参数为字符串,按每个字符打印
//打印机属于公共资源
func Printer(str string) {
for _, data := range str {
fmt.Printf("%c", data)
time.Sleep(time.Second)
}
fmt.Printf("\n")
}
//person1执行完后,才能到person2执行
func person1() {
Printer("hello")
ch <- 666 //给管道写数据,发送
}
func person2() {
<-ch //从管道取数据,接收,如果通道没有数据他就会阻塞
Printer("world")
}
func main() {
//新建2个协程,代表2个人,2个人同时使用打印机
go person1()
go person2()
//特地不让主协程结束,死循环
for {
}
}
09_通过channel实现同步和数据交互.go
package main
import (
"fmt"
"time"
)
func main() {
//创建channel
ch := make(chan string)
defer fmt.Println("主协程也结束")
go func() {
defer fmt.Println("子协程调用完毕")
for i := 0; i < 2; i++ {
fmt.Println("子协程 i = ", i)
time.Sleep(time.Second)
}
ch <- "我是子协程,要工作完毕"
}()
str := <-ch //没有数据前,阻塞
fmt.Println("str = ", str)
}
10_无缓冲的channel.go
package main
import (
"fmt"
"time"
)
func main() {
//创建一个无缓存的channel
ch := make(chan int, 0)
//len(ch)缓冲区剩余数据个数, cap(ch)缓冲区大小
fmt.Printf("len(ch) = %d, cap(ch)= %d\n", len(ch), cap(ch))
//新建协程
go func() {
for i := 0; i < 3; i++ {
fmt.Printf("子协程:i = %d\n", i)
ch <- i //往chan写内容
}
}()
//延时
time.Sleep(2 * time.Second)
for i := 0; i < 3; i++ {
num := <-ch //读管道中内容,没有内容前,阻塞
fmt.Println("num = ", num)
}
}
11_有缓冲的channel.go
package main
import (
"fmt"
"time"
)
func main() {
//创建一个有缓存的channel
ch := make(chan int, 3)
//len(ch)缓冲区剩余数据个数, cap(ch)缓冲区大小
fmt.Printf("len(ch) = %d, cap(ch)= %d\n", len(ch), cap(ch))
//新建协程
go func() {
for i := 0; i < 10; i++ {
ch <- i //往chan写内容
fmt.Printf("子协程[%d]: len(ch) = %d, cap(ch)= %d\n", i, len(ch), cap(ch))
}
}()
//延时
time.Sleep(2 * time.Second)
for i := 0; i < 10; i++ {
num := <-ch //读管道中内容,没有内容前,阻塞
fmt.Println("num = ", num)
}
}
12_关闭channel.go
package main
import (
"fmt"
)
func main() {
ch := make(chan int) //创建一个无缓存channel
//新建一个goroutine
go func() {
for i := 0; i < 5; i++ {
ch <- i //往通道写数据
}
//不需要再写数据时,关闭channel
close(ch)
//ch <- 666 //关闭channel后无法再发送数据
}() //别忘了()
for num := range ch {
fmt.Println("num = ", num)
}
}
func main01() {
ch := make(chan int) //创建一个无缓存channel
//新建一个goroutine
go func() {
for i := 0; i < 5; i++ {
ch <- i //往通道写数据
}
//不需要再写数据时,关闭channel
close(ch)
//ch <- 666 //关闭channel后无法再发送数据
}() //别忘了()
for {
//如果ok为true,说明管道没有关闭
if num, ok := <-ch; ok == true {
fmt.Println("num = ", num)
} else { //管道关闭
break
}
}
}
13_单向channel的特性.go
package main
//"fmt"
func main() {
//创建一个channel, 双向的
ch := make(chan int)
//双向channel能隐式转换为单向channel
var writeCh chan<- int = ch //只能写,不能读
var readCh <-chan int = ch //只能读,不能写
writeCh <- 666 //写
//<-writeCh //err, invalid operation: <-writeCh (receive from send-only type chan<- int)
<-readCh //读
//readCh <- 666 //写, err, invalid operation: readCh <- 666 (send to receive-only type <-chan int)
//单向无法转换为双向
//var ch2 chan int = writeCh //cannot use writeCh (type chan<- int) as type chan int in assignment
}
14_单向channel的应用.go
package main
import (
"fmt"
)
//此通道只能写,不能读
func producer(out chan<- int) {
for i := 0; i < 10; i++ {
out <- i * i
}
close(out)
}
//此channel只能读,不能写
func consumer(in <-chan int) {
for num := range in {
fmt.Println("num = ", num)
}
}
func main() {
//创建一个双向通道
ch := make(chan int)
//生产者,生产数字,写入channel
//新开一个协程
go producer(ch) //channel传参,引用传递
//消费者,从channel读取内容,打印
consumer(ch)
}
网友评论