I am new to Golang and trying to understand Concurrency and Parallel. I read below mentioned article about Concurrency and Parallel. I have executed same program. But not getting the same ( mixed letter & character ) output. Getting first all letters then characters. it seems like concurrency is working not Parallel is not.
Article says add runtime.GOMAXPROCS(4) to make parallel.
Why I am not getting parallel out ?
I am using 4 core CPU system and GO version 1.8.2
https://www.goinggo.net/2014/01/concurrency-goroutines-and-gomaxprocs.html
I know that if we add Sleep then i can see parallel output, as per Concurrency concept . But Parallelism says if system has more than one CPU, then each thread will run in one CPU and it becomes Parallel process. My question here is Why I am not getting parallel output though my system has 4 core and added runtime.GOMAXPROCS(4), .
Go Program
package main
import (
"fmt"
"runtime"
"sync"
)
func main() {
runtime.GOMAXPROCS(2)
var wg sync.WaitGroup
wg.Add(2)
fmt.Println("Starting Go Routines")
go func() {
defer wg.Done()
for char := ‘a’; char < ‘a’+26; char++ {
fmt.Printf("%c ", char)
}
}()
go func() {
defer wg.Done()
for number := 1; number < 27; number++ {
fmt.Printf("%d ", number)
}
}()
fmt.Println("Waiting To Finish")
wg.Wait()
fmt.Println("
Terminating Program")
}
My Output
Starting Go Routines
Waiting To Finish
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 a
b c d e f g h i j k l m n o p q r s t u v w x y z
Terminating Program
Expected Output ( not the same , But Parallel output )
Starting Go Routines
Waiting To Finish
a b 1 2 3 4 c d e f 5 g h 6 i 7 j 8 k 9 10 11 12 l m n o p q 13 r s 14
t 15 u v 16 w 17 x y 18 z 19 20 21 22 23 24 25 26
Terminating Program