zoukankan      html  css  js  c++  java
  • go sync.Pool 包简单总结

    // 自带pool 的用途总结 
    // 1. sync 中自带的 包没有控制 pool中对象的数量, 当你pool中存在你put之后的对象的时候,会复用你创建的。如果不存在,则会通过new去创建并且返回
    // 2. sync 中服用的大体都是结构体,缓存, 对于长链接。 他会不告知清理 所以又可能会重建
    package main import (
    "fmt" "sync" ) func main() { bPool := sync.Pool{ New: func() interface{} { return make([]int, 5) }, } get := bPool.Get().([]int) get[0] = 1 bPool.Put(get) // pool 中存在刚刚更改的对象直接获取服用 get0 := bPool.Get() fmt.Println("get0: ", get0) bPool.Put([]int{1,23,4,6,6}) // pool 中存在刚刚put的对象不需要创建 get1 := bPool.Get() fmt.Println("get1: ", get1) // pool 中不存在对象 需要new get2 := bPool.Get() fmt.Println("get2: ", get2) get3 := bPool.Get() fmt.Println("get3: ", get3) } ====> get0: [1 0 0 0 0] get1: [1 23 4 6 6] get2: [0 0 0 0 0] get3: [0 0 0 0 0]
    邮箱: 1090055252@qq.com
  • 相关阅读:
    输入挂
    最长递增子序列nlogn的做法
    lca 倍增模版
    讨厌字符串
    js的事件处理与闭包:
    http
    html的语义化
    js性能优化
    js的缓存
    字面量声明和函数式声明
  • 原文地址:https://www.cnblogs.com/zhaoxianxin/p/15745476.html
Copyright © 2011-2022 走看看