Created
January 9, 2022 15:52
-
-
Save mche/646954f3733674eee5883eff9b21a3f8 to your computer and use it in GitHub Desktop.
Доп. запуск горутин из цикла результатов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
//"math/rand" | |
) | |
func squares(i int, c chan int, semaphoreChan chan bool) { | |
semaphoreChan <- true | |
time.Sleep(time.Duration(10 * i) * time.Millisecond)//time.Duration(rand.Intn(500)*i) | |
c <- i | |
<- semaphoreChan | |
} | |
func main() { | |
fmt.Println("main() started") | |
c := make(chan int) | |
semaphoreChan := make(chan bool, 9) | |
defer func() { | |
close(c) | |
close(semaphoreChan) | |
}() | |
cnt := 0 | |
for i := 0; i < 10; i++ { | |
go squares(i, c, semaphoreChan) | |
} | |
sum := 0 | |
for val := range c { | |
fmt.Println(val) | |
cnt++ | |
sum += val | |
if cnt == 30 { | |
break | |
} else /*if val == 1*/ { | |
go squares(val*5+1, c, semaphoreChan) | |
} | |
/*else { | |
time.Sleep(300 * time.Millisecond) | |
go squares(val*10, c, semaphoreChan) | |
}*/ | |
} | |
fmt.Println("main() stopped", sum) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment