Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Concurrent-computing/Go/concurrent-computing-1.go
Normal file
23
Task/Concurrent-computing/Go/concurrent-computing-1.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/exp/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
words := []string{"Enjoy", "Rosetta", "Code"}
|
||||
seed := uint64(time.Now().UnixNano())
|
||||
q := make(chan string)
|
||||
for i, w := range words {
|
||||
go func(w string, seed uint64) {
|
||||
r := rand.New(rand.NewSource(seed))
|
||||
time.Sleep(time.Duration(r.Int63n(1e9)))
|
||||
q <- w
|
||||
}(w, seed+uint64(i))
|
||||
}
|
||||
for i := 0; i < len(words); i++ {
|
||||
fmt.Println(<-q)
|
||||
}
|
||||
}
|
||||
25
Task/Concurrent-computing/Go/concurrent-computing-2.go
Normal file
25
Task/Concurrent-computing/Go/concurrent-computing-2.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
words := []string{"Enjoy", "Rosetta", "Code"}
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
l := log.New(os.Stdout, "", 0)
|
||||
var q sync.WaitGroup
|
||||
q.Add(len(words))
|
||||
for _, w := range words {
|
||||
w := w
|
||||
time.AfterFunc(time.Duration(rand.Int63n(1e9)), func() {
|
||||
l.Println(w)
|
||||
q.Done()
|
||||
})
|
||||
}
|
||||
q.Wait()
|
||||
}
|
||||
25
Task/Concurrent-computing/Go/concurrent-computing-3.go
Normal file
25
Task/Concurrent-computing/Go/concurrent-computing-3.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
w1 := make(chan bool, 1)
|
||||
w2 := make(chan bool, 1)
|
||||
w3 := make(chan bool, 1)
|
||||
for i := 0; i < 3; i++ {
|
||||
w1 <- true
|
||||
w2 <- true
|
||||
w3 <- true
|
||||
fmt.Println()
|
||||
for i := 0; i < 3; i++ {
|
||||
select {
|
||||
case <-w1:
|
||||
fmt.Println("Enjoy")
|
||||
case <-w2:
|
||||
fmt.Println("Rosetta")
|
||||
case <-w3:
|
||||
fmt.Println("Code")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue