You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
496 B
32 lines
496 B
12 years ago
|
package main
|
||
|
|
||
|
import (
|
||
12 years ago
|
"fmt"
|
||
|
"math"
|
||
|
"time"
|
||
12 years ago
|
)
|
||
|
|
||
|
func sum(a []int, c chan int) {
|
||
12 years ago
|
sum := 0
|
||
|
for _, v := range a {
|
||
|
sum += v
|
||
|
}
|
||
|
to_sleep := time.Duration(100*math.Abs(float64(sum))) * time.Millisecond
|
||
|
fmt.Printf("Sleeping %s...\n", to_sleep)
|
||
|
time.Sleep(to_sleep)
|
||
|
c <- sum
|
||
12 years ago
|
}
|
||
|
|
||
|
func main() {
|
||
12 years ago
|
a := []int{7, 2, 8, -9, 4, 0}
|
||
12 years ago
|
|
||
12 years ago
|
c := make(chan int)
|
||
|
go sum(a[:len(a)/2], c)
|
||
|
go sum(a[len(a)/2:], c)
|
||
|
x := <-c
|
||
|
fmt.Printf("x = %d\n", x)
|
||
|
y := <-c
|
||
|
fmt.Printf("y = %d\n", y)
|
||
|
fmt.Println(x, y, x+y)
|
||
12 years ago
|
}
|