Last active
December 11, 2017 18:47
-
-
Save marlonfan/43967518265572510e0c8ad7b4cb084a 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" | |
"net/http" | |
"sync" | |
"time" | |
) | |
var count = 0 | |
var lock = new(sync.Mutex) | |
func main() { | |
startTime := time.Now() | |
maxTheads := make(chan bool, 500) | |
for i := 0; i < 100; i++ { | |
maxTheads <- true | |
fmt.Printf("第%d个go程要创建了-----------\n", i) | |
go func() { | |
res, err := http.Get("https://tfan.net") | |
if err != nil { | |
panic("崩溃%s" + err.Error()) | |
} | |
defer res.Body.Close() | |
lock.Lock() | |
count++ | |
fmt.Printf("输出内容到了,第%d次读取\n", count) | |
lock.Unlock() | |
<-maxTheads | |
}() | |
} | |
for i := 0; i < cap(maxTheads); i++ { | |
maxTheads <- true | |
} | |
elapsed := time.Since(startTime) | |
fmt.Printf("%s", elapsed) | |
} |
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" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
) | |
func main() { | |
shutdown := make(chan bool, 1) | |
for i := 0; i < 15; i++ { | |
go func() { | |
defer func() { | |
fmt.Printf("我走了!") | |
}() | |
for { | |
select { | |
case <-shutdown: | |
fmt.Printf("大Boss让我回去上班!-----------") | |
return | |
default: | |
fmt.Printf("我晃荡呀晃荡\n") | |
time.Sleep(time.Second * 1) | |
} | |
} | |
}() | |
} | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) | |
for sig := range c { | |
switch sig { | |
case syscall.SIGINT, syscall.SIGTERM: // 获取退出信号时,关闭globalQuit, 让所有监听者退出 | |
close(shutdown) | |
time.Sleep(1 * time.Second) | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment