Created
June 14, 2017 04:25
-
-
Save psankar/8af76ba183b0203ec141bca8156f5955 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" | |
"runtime" | |
"time" | |
) | |
type LED struct { | |
state bool | |
ticker *time.Ticker | |
} | |
func toggle(led *LED) { | |
led.state = !led.state | |
} | |
func looper(led *LED) { | |
for range led.ticker.C { | |
toggle(led) | |
} | |
} | |
func main() { | |
fmt.Println("Initial number of GoRoutines: ", runtime.NumGoroutine()) | |
led := &LED{state: true, ticker: time.NewTicker(time.Millisecond * 500)} | |
go looper(led) | |
fmt.Println("Number of GoRoutines after a call to looper: ", runtime.NumGoroutine()) | |
time.Sleep(2 * time.Second) | |
led.ticker.Stop() | |
fmt.Println("Number of GoRoutines after stopping the ticker: ", runtime.NumGoroutine()) | |
runtime.GC() | |
fmt.Println("Number of GoRoutines after gc: ", runtime.NumGoroutine()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment