package main
import (
"fmt"
"sync"
"time"
)
func main() {
t := time.NewTimer(3 * time.Second)
fmt.Printf("Start waiting at %v\n", time.Now().Format(time.UnixDate))
<-t.C
fmt.Printf("Code executed at %v\n", time.Now().Format(time.UnixDate))
wg := &sync.WaitGroup{}
wg.Add(1)
fmt.Printf("Start waiting for AfterFunc at %v\n", time.Now().Format(time.UnixDate))
time.AfterFunc(3*time.Second, func() {
fmt.Printf("Code executed for AfterFunc at %v\n", time.Now().Format(time.UnixDate))
wg.Done()
})
wg.Wait()
fmt.Printf("Waiting on time.After at %v\n", time.Now().Format(time.UnixDate))
<-time.After(3 * time.Second)
fmt.Printf("Code resumed at %v\n", time.Now().Format(time.UnixDate))
}
/*
Start waiting at Thu Mar 22 00:32:42 CST 2018
Code executed at Thu Mar 22 00:32:45 CST 2018
Start waiting for AfterFunc at Thu Mar 22 00:32:45 CST 2018
Code executed for AfterFunc at Thu Mar 22 00:32:48 CST 2018
Waiting on time.After at Thu Mar 22 00:32:48 CST 2018
Code resumed at Thu Mar 22 00:32:51 CST 2018
*/
原文地址:https://www.cnblogs.com/zrdpy/p/8620888.html
时间: 2024-10-07 23:39:42