惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
量子位
A
Arctic Wolf
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
博客园 - Franky
C
Cyber Attacks, Cyber Crime and Cyber Security
The Cloudflare Blog
Last Week in AI
Last Week in AI
The Hacker News
The Hacker News
I
Intezer
J
Java Code Geeks
P
Privacy International News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Secure Thoughts
Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
S
Securelist
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Jina AI
Jina AI
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
雷峰网
雷峰网
T
Tenable Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy & Cybersecurity Law Blog
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 【当耐特】
T
Threat Research - Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
N
News | PayPal Newsroom
Google Online Security Blog
Google Online Security Blog
K
Kaspersky official blog
H
Help Net Security
宝玉的分享
宝玉的分享
罗磊的独立博客
Webroot Blog
Webroot Blog
月光博客
月光博客
B
Blog RSS Feed
Recorded Future
Recorded Future

博客园 - CHHC

公钥解析 eSIM SGP32 自建符合GSMA规范的eIM平台(支持SGP32及SGP22卡接入) eSIM SGP32 eIM GSMA转码器(支持net及java) eSIM SGP32 证书 eSIM SGP32 生成eUICC所需的配置数据 eSIM SGP32 EuiccPackage包eimSignature和euiccSignEPR生成及校验 eSIM SGP32/SGP22 EUICC.SDK - IPAd RTSPShape-包含服务端及客户端 树莓派安装与配置 NetCore树莓派桌面应用程序 C#解析TLV数据(der -> asn1) golang 项目依赖备份 SGP32笔记 SoftSIM - swSIM eSIM SGP.22 LPA程序开发 - 协议解析 eSIM SGP.22 LPA程序开发 - 实现功能 PGP文件加解密 vscode配置c/c++环境 网关开发笔记 电池笔记 SQLiteRedis - 静动态数据联合使用 RTP推流测试 SQLite批量操作优化方案 工业状态控制 vSIM / SoftSIM笔记 移远AT指令笔记
golang优化
CHHC · 2025-10-14 · via 博客园 - CHHC

工作池模式(提交10个任务给n个goroutine池处理)

package main

import (
    "fmt"
    "sync"
    "time"

    "github.com/panjf2000/ants"
)

// 模拟一个需要被处理的任务
func myTask(callName string, i int) {
    time.Sleep(100 * time.Millisecond) // 模拟任务处理耗时
    fmt.Printf("%s processing task %d\n", callName, i)
}

func main() {
    // 普通goroutine写法,创建10个goroutine处理任务
    for i := 0; i < 10; i++ {
        go func() {
            myTask("goroutine", i)
        }()
    }

    // 提交10个任务给n个goroutine池处理
    pool, err := ants.NewPool(5) // 创建固定大小的ants池(n个goroutine)
    if err != nil {
        fmt.Printf("Failed to create pool: %v\n", err)
        return
    }
    defer pool.Release()
    var wg sync.WaitGroup
    for i := 0; i < 10; i++ {
        wg.Add(1)
        task := func() {
            defer wg.Done()
            myTask("antsPool", i)
        }
        pool.Submit(task)
    }
    wg.Wait()
}

对象池模式(对象复用)

package main

import (
    "bytes"
    "fmt"
    "sync"
)

type Data struct {
    Value int
}

func createData() *Data {
    return &Data{Value: 42}
}

var dataPool = sync.Pool{
    New: func() any {
        return &Data{}
    },
}

var bufferPool = sync.Pool{
    New: func() any {
        return new(bytes.Buffer)
    },
}

func main() {
    for i := 0; i < 10; i++ {
        // 普通对象初始化
        obj1 := createData()
        fmt.Println(obj1.Value)

        // 使用对象池化 -- sync.Pool,使用场景:短期存活、可复用对象
        obj2 := dataPool.Get().(*Data)
        obj2.Value = 42
        fmt.Println(obj2.Value)
        dataPool.Put(obj2)
    }

    // 池化字节缓冲区
    buf := bufferPool.Get().(*bytes.Buffer)
    buf.Reset()
    buf.WriteString("Hello, pooled world!")
    fmt.Println(buf.String())
    bufferPool.Put(buf)
}