Created
February 24, 2016 09:04
-
-
Save longkey1/44d266833a33436dc8f6 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" | |
"sync" | |
"time" | |
"math/rand" | |
) | |
type member struct { | |
name string | |
isOchi bool | |
} | |
func main() { | |
var furi sync.WaitGroup | |
var boke sync.WaitGroup | |
var act sync.WaitGroup | |
for _, m := range members() { | |
if m.isOchi == false { | |
furi.Add(1) | |
} else { | |
boke.Add(1) | |
} | |
act.Add(1) | |
} | |
for _, m := range members() { | |
go action(m, furi, boke, act) | |
} | |
act.Wait() | |
} | |
func action(m member, furi sync.WaitGroup, boke sync.WaitGroup, act sync.WaitGroup) { | |
defer act.Done() | |
if false == m.isOchi { | |
time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond) | |
fmt.Printf("%s: 私がやります!\n", m.name) | |
furi.Done() | |
boke.Wait() | |
fmt.Printf("%s: どうぞどうぞ!\n", m.name) | |
} else { | |
furi.Wait() | |
fmt.Printf("%s: じゃあ、私がやります・・・\n", m.name) | |
boke.Done() | |
} | |
} | |
func members() []member { | |
return []member{ | |
member{"higo", false}, | |
member{"jimon", false}, | |
member{"ryuhei", true}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment