package main
import (
\"fmt\"
\"math/rand\"
\"sync\"
\"time\"
)
var wg sync.WaitGroup
// Ji Suan Yi Ge 64 Wei Sui Ji Shu De Ge Wei De He
func randNumber(x int64) int64 {
var sum int64 = 0
for x > 0 {
a := x % 10
x = x / 10
sum += a
}
return sum
}
func main() {
wg.Add(25)
var jobChan = make(chan int64, 10)
var resultChan = make(chan int64, 10)
go func(jobChan chan<- int64, ) {
defer wg.Done()
for i:=0;i<1000;i++{
rand.Seed(time.Now().UnixNano())
jobChan <- rand.Int63n(100)
}
}(jobChan)
for i:=0;i<24;i++{
go func(jobChan <-chan int64, resultChan chan<- int64, ) {
defer wg.Done()
for num:= range jobChan{
resultChan <- randNumber(num)
}
}(jobChan, resultChan)
}
for i:= range resultChan{
fmt.Println(i)
}
wg.Wait()
}
Fa Sheng Cuo Wu :fatal error: all goroutines are asleep - deadlock!
Qing Jiao Yi Xia , Wei Shi Me Hui Fa Sheng Si Suo De Qing Kuang ?
“`goPackage Main
Import (
“Fmt “
“Math/rand “
“Sync “
“Time “
“Time “
Var Wg Sync.WaitGroup
/ / computation of an everybody of 64 random number andFunc RandNumber(x Int64) Int64 {Var Sum Int64 = 0For X>0 {A:=X % 10X = X / 10Sum += A
}Return Sum
}
Func Main() {Wg.Add(25)Var JobChan = Make(chan Int64, 10)Var ResultChan = Make(chan Int64, 10)Go Func(jobChan Chan<- Int64, ) {Defer Wg.Done()For I:=0; I<1000; I++ {Rand.Seed(time.Now().UnixNano() )JobChan <- Rand.Int63n(100)
}Close(jobChan) // here should shut JobChan, all over the meeting in all previous in the Range of JobChan otherwise all the time block
}(jobChan)
For I:=0; I<24; I++ {Go Func(jobChan <-chan Int64, resultChan Chan<- Int64, ) {Defer Wg.Done()For Num:=Range JobChan {ResultChan <- RandNumber(num)
}
}(jobChan, resultChan)
}
/ / in alling over all previous to put a Goroutine, carry out of here ResultChan, after the Wait that lets Wg is carried out advocate assist Cheng is exited directly
/ / if be put in advocate assist in Cheng, also did not shut the word of ResultChan, also can create block, can produce dead lockGo Func() {For I:=Range ResultChan {Fmt.Println(i)
}
}()Wg.Wait()
}
“`
Go seeing Waitgroup implementation
Channel was not shutFor Num:=Range JobChan
Won’t exit forever
For Num:=Range JobChan{ResultChan <- RandNumber(num)
}Job 1000 after be being sent, still get stuck all the time here advocate.
Final Wait() also does not have a meaning, from ResultChan read over there also retreat do not come out
The reason is 2l say in that way, be without Close block because of JobChan and ResultChan in the loop, jobChan is handled in that way like 2l, the processing of ResultChan, can be behind Wg.Wait() Close ResultChan, next again Range ResultChan is printed as a result
@linjunyi22 acknowledgment, just learned Go, a few concepts change do not clear. But you this kind of method seems to be able to be caused receive can exit not completely. Myself also solved this problem.
“`Package Main
Import (
“Context “
“Fmt “
“Math/rand “
“Sync “
“Time “
“Time “
Var (Wg = Sync.WaitGroup{} // is used at computation, make a program normal carry out, won’t advocate executive Wan Zihan counts function to had been not carried out. Computation Qing Dynasty need not await 0 timesA Int
/ / Lock Sync.MutexRwlock Sync.RWMutexIcons Map[string]stringLoadIconsOnce Sync.Once
/ / M Sync.Map
/ / M Sync.Map
Func RandNumber(x Int64) Int64 {Var Sum Int64 = 0For X>0 {A:=X % 10X = X / 10Sum += A
}Return Sum
}
Func Main() {Wg.Add(25)Defer Wg.Wait()Var MaxSend = 10Var JobChan = Make(chan Int64, 10)Var ResultChan = Make(chan Int64, 10)Var Lock Sync.MutexCtx, cancel:=Context.WithCancel(context.Background() )
/ / Var Once Sync.OnceGo Func(jobChan Chan<- Int64, ) {For I:=0;i<maxSend;i++{Rand.Seed(time.Now().UnixNano() )JobChan <- Rand.Int63n(100)
}Close(jobChan)Wg.Done()
}(jobChan)
Count1:=1For I:=0;i<24;i++{Go Func(jobChan <-chan Int64, resultChan Chan Int64, ctx Context.Context) {Defer Wg.Done()For Num:=Range JobChan{Select {Case <- Ctx.Done() : ReturnCase ResultChan <- RandNumber(num) : Lock.Lock()Fmt.Println(“count:” , count1)Count1 += 1Lock.Unlock()
}
}
}(jobChan, resultChan, ctx)
}
Num:=MaxSendCount:=1For Value:=Range ResultChan {If Num==1 {Fmt.Printf(“key:%V, value:%V\n” , count, value)Cancel()Return
}else {Num -= 1Fmt.Printf(“key:%V, value:%V\n” , count, value)Count += 1
}
}
}
“`
In the reply, write code how Md not become effective = =