Comments (7)
对于这种的解决方法,我一直有一个问题,为什么最后的goruntine会最先执行,下面是代码:
package main
import (
"runtime"
"sync"
"fmt"
)
const N = 10
func main() {
runtime.GOMAXPROCS(1)
wg := &sync.WaitGroup{}
wg.Add(N*2)
for i := 0; i < N; i++ {
m := i
go func(m int) {
defer wg.Done()
fmt.Print(m)
}(m)
go func(m int) {
defer wg.Done()
runtime.Gosched()
fmt.Printf(`%c`,'A'+m)
}(m)
}
wg.Wait()
}
结果是J0123456789ABCDEFGHI
J居然是最先打印的,大佬知道原因么,谢谢!
from interview-go.
貌似是golang的调度器并不能保证协程是顺序执行的。
from interview-go.
也贴一个我的:
https://gist.github.com/tlightsky/a67e3ee1360e79432478d0baa4a3cab6
from interview-go.
package main
import (
"fmt"
)
// POINT: communicate between goroutines by channel
//使用两个 goroutine 交替打印序列,一个 goroutine 打印数字, 另外一个 goroutine 打印字母, 最终效果如下:
//12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ2728
func AlterPrint(){
letter, number := make(chan bool), make(chan bool)
letterDone := make(chan bool)
go func() {
i := 1
for {
select{
case <-number: {
fmt.Print(i)
i++
fmt.Print(i)
i++
letter <- true
break
}
default: {
break
}
}
}
}()
go func(){
i := 'A'
for {
if i > 'Z' {
letterDone <- true
return
}
select{
case <-letter: {
fmt.Print(string(i))
i++
fmt.Print(string(i))
i++
number <- true
break
}
default: {
break
}
}
}
}()
number <- true
<- letterDone
}
from interview-go.
letter, number := make(chan struct{}), make(chan struct{})
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
i := 1
pr := func() {
fmt.Print(i)
i++
}
for {
_, ok := <-number
if !ok {
close(letter)
break
}
pr()
pr()
letter <- struct{}{}
}
}()
go func() {
defer wg.Done()
str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
length := len(str)
i := 0
pr := func() bool {
if i == length {
return false
}
fmt.Print(string(str[i]))
i++
return true
}
for {
_, ok := <-letter
if !ok || !pr() || !pr() {
break
}
number <- struct{}{}
}
close(number)
}()
number <- struct{}{}
wg.Wait()
from interview-go.
贴一个俺的😛
func main() {
letter, number := make(chan int, 1), make(chan int, 1)
var wg sync.WaitGroup
wg.Add(2)
defer func() {
close(letter)
close(number)
}()
go func() {
for {
select {
case num := <-number:
if num >= 13 {
wg.Done()
return
}
fmt.Printf("%d%d", 2*num+1, 2*num+2)
letter <- num
}
}
}()
go func() {
for {
select {
case let := <-letter:
fmt.Printf("%c%c", 'A'+2*let, 'A'+2*let+1)
number <- let + 1
if let >= 12 {
wg.Done()
return
}
}
}
}()
number <- 0
wg.Wait()
}
from interview-go.
package main import ( "fmt" "runtime" "sync" ) const N = 28 func main() { runtime.GOMAXPROCS(1) wg := &sync.WaitGroup{} wg.Add(N) for i := 0; i < N; i++ { go func(m int) { defer wg.Done() fmt.Printf(`%d%d`,m+1,m+2) runtime.Gosched() }(i) go func(m int) { defer wg.Done() if m == 26 { runtime.Goexit() } fmt.Printf(`%c%c`, 'A'+m, 'A'+m+1) }(i) i = i + 1 } wg.Wait() }
总共这么一个需求犯得着在for循环里面放goroutine么,这个不是禁忌么
from interview-go.
Related Issues (20)
- 实现 Sunday 匹配 精简版代码
- 第三题的答案是否使用了额外的空间 HOT 1
- gpm窃取过程不对 HOT 1
- 常见语法题目 一 的第6题,CAS操作 HOT 3
- 一些建议 HOT 4
- 语法找错题 最后一道解答错误
- question17/多协程切片查询
- 最长公共前缀 go版本代码有误
- 第六题机器人坐标计算有问题
- 冒泡排序完全是错的 HOT 1
- “实现阻塞读且并发安全的map”答案补充 HOT 1
- q001中的chan类型应该用struct{}
- 实现 Sunday 匹配 needle在最末尾的时候返回结果异常
- 读阻塞且并发安全的 Map,答案是否有问题?
- 最接近的三数之和,代码有误
- Goroutine泄露问题——交替打印数字和字母
- q006 机器人坐标,L 跟 R指令是不是调转了
- 判断字符串中字符是否全都不同为什么有个127的判断?
- 『goroutine和channel使用一』 答案协程泄露
- interview-go/question /q015.md
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interview-go.