-
Notifications
You must be signed in to change notification settings - Fork 2
/
example2.go
72 lines (54 loc) · 1.45 KB
/
example2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2014 Ardan Studios
//
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// $ ./example2 | cut -c1 | grep '[AB]' | uniq
// Sample program to show how the goroutine scheduler
// will time slice goroutines on a single thread.
package main
import (
"crypto/sha1"
"fmt"
"runtime"
"strconv"
"sync"
)
func init() {
// Allocate one logical processor for the scheduler to use.
runtime.GOMAXPROCS(1)
}
func main() {
// wg is used to manage concurrency.
var wg sync.WaitGroup
wg.Add(2)
fmt.Println("Create Goroutines")
// Create the first goroutine and manage its lifecycle here.
go func() {
printHashes("A")
wg.Done()
}()
// Create the second goroutine and manage its lifecycle here.
go func() {
printHashes("B")
wg.Done()
}()
// Wait for the goroutines to finish.
fmt.Println("Waiting To Finish")
wg.Wait()
fmt.Println("Terminating Program")
}
// printHashes calculates the sha1 hash for a range of
// numbers and prints each in hex encoding.
func printHashes(prefix string) {
// print each has from 1 to 10. Change this to 50000 and
// see how the scheduler behaves.
for i := 1; i <= 50000; i++ {
// Convert i to a string.
num := strconv.Itoa(i)
// Calculate hash for string num.
sum := sha1.Sum([]byte(num))
// Print prefix: 5-digit-number: hex encoded hash
fmt.Printf("%s: %05d: %x\n", prefix, i, sum)
}
fmt.Println("Completed", prefix)
}