代码拉取完成,页面将自动刷新
package coroutine
import (
"fmt"
"gitee.com/quant1x/gox/concurrent"
"strconv"
"testing"
"time"
)
var (
once PeriodicOnce
//cache = map[string]int{}
//cache = cmap.NewStringMap[int]()
cache = concurrent.NewHashMap[string, int]()
)
func lazyInit() {
for i := 0; i < 5; i++ {
k := strconv.Itoa(i)
//cache[k] = i
cache.Set(k, i)
fmt.Println("reset", k, "=>", i)
}
}
func getInt(key string) (int, bool) {
once.Do(lazyInit)
//v, ok := cache[key]
v, ok := cache.Get(key)
return v, ok
}
func setInt(key string, value int) {
once.Do(lazyInit)
//cache[key] = value
cache.Set(key, value)
}
func TestPeriodicOnce(t *testing.T) {
rwCount := 1000
producer := func() {
for i := 0; i < rwCount; i++ {
k := strconv.Itoa(i % 5)
setInt(k, i)
fmt.Println(k, "=>", i)
time.Sleep(time.Millisecond * 10)
}
}
reader := func() {
for i := 0; i < rwCount; i++ {
k := strconv.Itoa(i % 5)
v, ok := getInt(k)
fmt.Println(v, "<=", i, ":", ok)
_ = v
_ = ok
time.Sleep(time.Millisecond * 10)
}
}
go producer()
go reader()
count := 60
for i := 0; i < count; i++ {
//once.Reset()
fmt.Println("--------------------")
time.Sleep(time.Second)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。