01_創(chuàng)建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() //新建一個協(xié)程昭灵, 新建一個任務(wù)
for {
fmt.Println("this is a main goroutine")
time.Sleep(time.Second) //延時1s
}
}
02_主goroutine先退出.go
package main
import (
"fmt"
"time"
)
//主協(xié)程退出了,其它子協(xié)程也要跟著退出
func main() {
go func() {
i := 0
for {
i++
fmt.Println("子協(xié)程 i = ", i)
time.Sleep(time.Second)
}
}() //別忘了()
i := 0
for {
i++
fmt.Println("main i = ", i)
time.Sleep(time.Second)
if i == 2 {
break
}
}
}
03_主協(xié)程先退出導(dǎo)致子協(xié)程沒有來得及調(diào)用.go
package main
import (
"fmt"
"time"
)
//主協(xié)程退出了佳遂,其它子協(xié)程也要跟著退出
func main() {
go func() {
i := 0
for {
i++
fmt.Println("子協(xié)程 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++ {
//讓出時間片蚤蔓,先讓別的協(xié)議執(zhí)行吊奢,它執(zhí)行完,再回來執(zhí)行此協(xié)程
runtime.Gosched()
fmt.Println("hello")
}
}
05_Goexit的使用.go
package main
import (
"fmt"
"runtime"
)
func test() {
defer fmt.Println("ccccccccccccc")
//return //終止此函數(shù)
runtime.Goexit() //終止所在的協(xié)程
fmt.Println("dddddddddddddddddddddd")
}
func main() {
//創(chuàng)建新建的協(xié)程
go func() {
fmt.Println("aaaaaaaaaaaaaaaaaa")
//調(diào)用了別的函數(shù)
test()
fmt.Println("bbbbbbbbbbbbbbbbbbb")
}() //別忘了()
//特地寫一個死循環(huán),目的不讓主協(xié)程結(jié)束
for {
}
}
06_GOMAXPROCS的使用.go
package main
import (
"fmt"
"runtime"
)
func main() {
//n := runtime.GOMAXPROCS(1) //指定以1核運(yùn)算
n := runtime.GOMAXPROCS(4) //指定以4核運(yùn)算
fmt.Println("n = ", n)
for {
go fmt.Print(1)
fmt.Print(0)
}
}
07_多任務(wù)資源競爭問題.go
package main
import (
"fmt"
"time"
)
//定義一個打印機(jī)丁寄,參數(shù)為字符串,按每個字符打印
//打印機(jī)屬于公共資源
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個協(xié)程泊愧,代表2個人伊磺,2個人同時使用打印機(jī)
go person1()
go person2()
//特地不讓主協(xié)程結(jié)束,死循環(huán)
for {
}
}
08_通過channel實(shí)現(xiàn)同步.go
package main
import (
"fmt"
"time"
)
//全局變量删咱,創(chuàng)建一個channel
var ch = make(chan int)
//定義一個打印機(jī)屑埋,參數(shù)為字符串,按每個字符打印
//打印機(jī)屬于公共資源
func Printer(str string) {
for _, data := range str {
fmt.Printf("%c", data)
time.Sleep(time.Second)
}
fmt.Printf("\n")
}
//person1執(zhí)行完后痰滋,才能到person2執(zhí)行
func person1() {
Printer("hello")
ch <- 666 //給管道寫數(shù)據(jù)摘能,發(fā)送
}
func person2() {
<-ch //從管道取數(shù)據(jù),接收敲街,如果通道沒有數(shù)據(jù)他就會阻塞
Printer("world")
}
func main() {
//新建2個協(xié)程徊哑,代表2個人,2個人同時使用打印機(jī)
go person1()
go person2()
//特地不讓主協(xié)程結(jié)束聪富,死循環(huán)
for {
}
}
09_通過channel實(shí)現(xiàn)同步和數(shù)據(jù)交互.go
package main
import (
"fmt"
"time"
)
func main() {
//創(chuàng)建channel
ch := make(chan string)
defer fmt.Println("主協(xié)程也結(jié)束")
go func() {
defer fmt.Println("子協(xié)程調(diào)用完畢")
for i := 0; i < 2; i++ {
fmt.Println("子協(xié)程 i = ", i)
time.Sleep(time.Second)
}
ch <- "我是子協(xié)程莺丑,要工作完畢"
}()
str := <-ch //沒有數(shù)據(jù)前,阻塞
fmt.Println("str = ", str)
}
10_無緩沖的channel.go
package main
import (
"fmt"
"time"
)
func main() {
//創(chuàng)建一個無緩存的channel
ch := make(chan int, 0)
//len(ch)緩沖區(qū)剩余數(shù)據(jù)個數(shù), cap(ch)緩沖區(qū)大小
fmt.Printf("len(ch) = %d, cap(ch)= %d\n", len(ch), cap(ch))
//新建協(xié)程
go func() {
for i := 0; i < 3; i++ {
fmt.Printf("子協(xié)程:i = %d\n", i)
ch <- i //往chan寫內(nèi)容
}
}()
//延時
time.Sleep(2 * time.Second)
for i := 0; i < 3; i++ {
num := <-ch //讀管道中內(nèi)容梢莽,沒有內(nèi)容前萧豆,阻塞
fmt.Println("num = ", num)
}
}
11_有緩沖的channel.go
package main
import (
"fmt"
"time"
)
func main() {
//創(chuàng)建一個有緩存的channel
ch := make(chan int, 3)
//len(ch)緩沖區(qū)剩余數(shù)據(jù)個數(shù), cap(ch)緩沖區(qū)大小
fmt.Printf("len(ch) = %d, cap(ch)= %d\n", len(ch), cap(ch))
//新建協(xié)程
go func() {
for i := 0; i < 10; i++ {
ch <- i //往chan寫內(nèi)容
fmt.Printf("子協(xié)程[%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 //讀管道中內(nèi)容昏名,沒有內(nèi)容前涮雷,阻塞
fmt.Println("num = ", num)
}
}
12_關(guān)閉channel.go
package main
import (
"fmt"
)
func main() {
ch := make(chan int) //創(chuàng)建一個無緩存channel
//新建一個goroutine
go func() {
for i := 0; i < 5; i++ {
ch <- i //往通道寫數(shù)據(jù)
}
//不需要再寫數(shù)據(jù)時,關(guān)閉channel
close(ch)
//ch <- 666 //關(guān)閉channel后無法再發(fā)送數(shù)據(jù)
}() //別忘了()
for num := range ch {
fmt.Println("num = ", num)
}
}
func main01() {
ch := make(chan int) //創(chuàng)建一個無緩存channel
//新建一個goroutine
go func() {
for i := 0; i < 5; i++ {
ch <- i //往通道寫數(shù)據(jù)
}
//不需要再寫數(shù)據(jù)時轻局,關(guān)閉channel
close(ch)
//ch <- 666 //關(guān)閉channel后無法再發(fā)送數(shù)據(jù)
}() //別忘了()
for {
//如果ok為true洪鸭,說明管道沒有關(guān)閉
if num, ok := <-ch; ok == true {
fmt.Println("num = ", num)
} else { //管道關(guān)閉
break
}
}
}
13_單向channel的特性.go
package main
//"fmt"
func main() {
//創(chuàng)建一個channel, 雙向的
ch := make(chan int)
//雙向channel能隱式轉(zhuǎn)換為單向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)
//單向無法轉(zhuǎn)換為雙向
//var ch2 chan int = writeCh //cannot use writeCh (type chan<- int) as type chan int in assignment
}
14_單向channel的應(yīng)用.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() {
//創(chuàng)建一個雙向通道
ch := make(chan int)
//生產(chǎn)者蜓竹,生產(chǎn)數(shù)字,寫入channel
//新開一個協(xié)程
go producer(ch) //channel傳參储藐,引用傳遞
//消費(fèi)者俱济,從channel讀取內(nèi)容,打印
consumer(ch)
}