Created
December 3, 2018 02:41
-
-
Save Niraj-Fonseka/d290598608446d0afb4dbfcf7e59499e 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" | |
) | |
var wg sync.WaitGroup | |
func main() { | |
start := time.Now() | |
wg.Add(4) | |
firstchan := make(chan string) | |
secondchan := make(chan string) | |
thirdchan := make(chan string) | |
fourthchan := make(chan string) | |
go FourthFunc(fourthchan) | |
go SecondFunc(secondchan) | |
go FirstFunc(firstchan) | |
go ThirdFunc(thirdchan) | |
fmt.Println(<-firstchan) | |
fmt.Println(<-secondchan) | |
fmt.Println(<-thirdchan) | |
fmt.Println(<-fourthchan) | |
wg.Wait() | |
fmt.Printf("Total time to finish : %s \n", time.Since(start).String()) | |
} | |
func FirstFunc(ch chan<- string) { | |
fmt.Println("-- Executing first function --") | |
time.Sleep(7 * time.Second) | |
defer wg.Done() | |
ch <- "-- First Function finished --" | |
} | |
func SecondFunc(ch chan<- string) { | |
fmt.Println("-- Executing second function --") | |
time.Sleep(5 * time.Second) | |
defer wg.Done() | |
ch <- "-- Second Function finished --" | |
} | |
func ThirdFunc(ch chan<- string) { | |
fmt.Println("-- Executing third function --") | |
time.Sleep(2 * time.Second) | |
defer wg.Done() | |
ch <- "-- Third Function finished --" | |
} | |
func FourthFunc(ch chan<- string) { | |
fmt.Println("-- Executing fourth function --") | |
time.Sleep(10 * time.Second) | |
defer wg.Done() | |
ch <- "-- Fourth Function finished --" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment