mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-24 21:46:03 +03:00
added ping pong example
This commit is contained in:
parent
fc69bc264a
commit
1d6b444d01
27
concurrency/pingpong/main.go
Normal file
27
concurrency/pingpong/main.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Ball struct{ hits int }
|
||||
|
||||
func main() {
|
||||
table := make(chan *Ball)
|
||||
go player("ping", table)
|
||||
go player("pong", table)
|
||||
table <- new(Ball) // game on; toss the ball
|
||||
time.Sleep(1 * time.Second)
|
||||
<-table // game over; grab the ball
|
||||
}
|
||||
|
||||
func player(name string, table chan *Ball) {
|
||||
for {
|
||||
ball := <-table
|
||||
ball.hits++
|
||||
fmt.Println(name, ball.hits)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
table <- ball
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user