想了解第一个goroutine示例,奇怪的结果的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于2022-07-18:以下go语言代码输出什么?A:Groutine;B:Main;C:G
想了解第一个goroutine示例,奇怪的结果的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于2022-07-18:以下go语言代码输出什么?A:Groutine;B:Main;C:Goroutine;D:GoroutineMain。 package main import ( “f、go 实现两个goroutine交替执行打印数字和字母、Golang goroutine协程(一) 编写第一个并发处理的程序、Golang | 同时结束多个goroutines的新知识。
本文目录一览:- 第一个goroutine示例,奇怪的结果
- 2022-07-18:以下go语言代码输出什么?A:Groutine;B:Main;C:Goroutine;D:GoroutineMain。 package main import ( “f
- go 实现两个goroutine交替执行打印数字和字母
- Golang goroutine协程(一) 编写第一个并发处理的程序
- Golang | 同时结束多个goroutines
第一个goroutine示例,奇怪的结果
此示例取自tour.golang.org/#63
package mainimport ( "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")}
输出
helloworldhelloworldhelloworldhelloworldhello
为什么world
只打印4
次数而不是5
?
编辑: 答案可以引自golang规范:
程序执行首先初始化主程序包,然后调用函数main。当函数main返回时,程序退出。它不等待其他(非主)goroutine完成。
答案1
小编典典当您的主要功能结束时,程序即结束,即所有goroutine均终止。您的主体在gosay("world")
完成之前会终止。如果您在主课程结束时睡了一段时间,您应该会看到最后的世界。
2022-07-18:以下go语言代码输出什么?A:Groutine;B:Main;C:Goroutine;D:GoroutineMain。 package main import ( “f
2022-07-18:以下go语言代码输出什么?A:Groutine;B:Main;C:Goroutine;D:GoroutineMain。
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan bool)
go func() {
<-ch
fmt.Print("Goroutine")
}()
time.Sleep(2 * time.Second)
close(ch)
time.Sleep(3 * time.Second)
fmt.Print("Main")
}
答案2022-07-18:
答案选D。 close channel 后,<-ch 会立马返回。
go 实现两个goroutine交替执行打印数字和字母
package main import ( "fmt" "sync" ) var wg sync.WaitGroup func letter(ch chan string) { defer wg.Done() for i:=0; i<26; i++{ ch <- fmt.Sprintf("%c", 'A'+i) } close(ch) } func number(ch chan int) { defer wg.Done() for i:=1; i<29; i++{ ch <- i } close(ch) } func main() { leChar := make(chan string, 26) intChar := make(chan int, 28) wg.Add(3) go letter(leChar) go number(intChar) go func() { defer wg.Done() for i := range intChar{ fmt.Printf("%d%d%s%s",i,<-intChar, <-leChar, <-leChar) } }() wg.Wait() }
Golang goroutine协程(一) 编写第一个并发处理的程序
package mainimport (
"fmt"
"runtime"
"sync"
)
var wg sync.WaitGroup //它是一个计数的信号量
func SayHi(s string) {
defer wg.Done() //每一个goroutine的函数执行完之后,就调用Done方法对计数器减1
for i := 0; i < 50000; i++ {
//runtime.Gosched()表示让cpu把时间片让给别人,下次某个时候继续恢复执行该goroutine
runtime.Gosched()
fmt.Println(i,s)
}
}
func main() {
//要根据电脑的实际物理核数调整参数4的大小,如果不是多核的,设置再多的逻辑处理器个数也没用
runtime.GOMAXPROCS(4) //4表示设置的逻辑处理器个数,4 ≤ runtime.Numcpu(),其中Numcpu()是cpu的物理内核个数
wg.Add(2) //先是使用Add方法设置计数器为2
go SayHi("world")
go SayHi("hello")
//Wait方法如果计数器大于0,就会阻塞主进程结束
//假如主进程结束了,上面的go协程还没来得及输出一个结果,整个程序就运行结束了
wg.Wait()
fmt.Println("finished") //main函数里执行的代码都是主进程代码
}
[root@contoso100 ~]# tail -2000 hello.txt|awk '{print NR,$0}'|xargs -n12 ##快速查看hello.txt最后2000行输出结果
[root@contoso100 ~]# awk '{printf"%s %s\t\t%s",NR,$0,NR%5?"":"\n"}' hello.txt
[root@contoso100 ~]#cat > myprint.format
{ if(NR%4 == 0)
printf("%s%s",NR" ",$0 "\n")
else
printf("%s%s",$0 "\t""\t")
}
[root@contoso100 ~]# more myprint.format
{ if(NR%4 == 0)
printf("%s%s",$0 "\t""\t")
}
[root@contoso100 ~]# awk -f myprint.format hello.txt
Golang | 同时结束多个goroutines
https://chilts.org/2017/06/12/cancelling-multiple-goroutines
关于第一个goroutine示例,奇怪的结果的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于2022-07-18:以下go语言代码输出什么?A:Groutine;B:Main;C:Goroutine;D:GoroutineMain。 package main import ( “f、go 实现两个goroutine交替执行打印数字和字母、Golang goroutine协程(一) 编写第一个并发处理的程序、Golang | 同时结束多个goroutines的相关信息,请在本站寻找。
本文标签: