Created
January 28, 2018 11:45
-
-
Save StarpTech/342dabb9b39126bc9de8d9e179becde9 to your computer and use it in GitHub Desktop.
When a channel is closed all buffered and blocked messages are proceed until the "range is finish
This file contains hidden or 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" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
ch := make(chan int, 100) | |
wg.Add(2) | |
go func() { | |
defer wg.Done() | |
for a := range ch { | |
fmt.Println("Receive job in Worker 1", a) | |
time.Sleep(time.Millisecond * 100) | |
} | |
}() | |
go func() { | |
defer wg.Done() | |
for a := range ch { | |
fmt.Println("Receive job in Worker 2", a) | |
time.Sleep(time.Millisecond * 100) | |
} | |
}() | |
for i := 0; i < 100; i++ { | |
ch <- i | |
fmt.Println("sent job", i) | |
} | |
close(ch) | |
wg.Wait() | |
fmt.Println("Done!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment