zoukankan      html  css  js  c++  java
  • 实现go并发的三种方式

    package main
    
    import (
        "fmt"
        "sync"
        "time"
    )
    
    // 方法一: 通过sync.WaitGroup + chan自定义
    
    var tokens chan struct{}
    var wg sync.WaitGroup
    
    func fprint(num int) {
    
        defer wg.Done()
        time.Sleep(10 * time.Microsecond)
        <-tokens
    
        fmt.Println(num)
    
    }
    
    func main() {
        t1 := time.Now()
        tokens = make(chan struct{}, 500) // 并发数量
    
        for num := 0; num < 100000; num++ {
    
            fmt.Printf("插入一个%d
    ", num)
            wg.Add(1)
            tokens <- struct{}{}
            // fmt.Printf("队列长度----%d
    ", len(tokens))
            go fprint(num)
    
        }
        wg.Wait()
        t := time.Since(t1)
        fmt.Println(t)
    
    }
    package main
    
    import (
        "fmt"
        "sync"
    
        "time"
    
        "github.com/panjf2000/ants/v2"
    )
    
    func myFunc(num int32) {
        time.Sleep(10 * time.Microsecond)
    
        fmt.Println(num)
    }
    
    func demoFunc(num int32) {
        time.Sleep(10 * time.Microsecond)
    
        fmt.Println(num)
    }
    
    func main() {
        t1 := time.Now()
        defer ants.Release()
    
        runTimes := 100000
        var wg sync.WaitGroup
    
        //方法二:使用ants默认数量的goroutinues,打开源码可以看到链接池的容量大小为: math.MaxInt32*(2147483647)
        // Use the common pool.
        syncCalculateSum := func(num int32) func() {
            return func() {
                demoFunc(num)
                fmt.Printf("running goroutines: %d
    ", ants.Running())
                wg.Done()
            }
    
        }
        for i := 0; i < runTimes; i++ {
            wg.Add(1)
            _ = ants.Submit(syncCalculateSum(int32(i))) //ants.Submit只接收func类型的参数
        }
        wg.Wait()
        // fmt.Printf("running goroutines: %d
    ", ants.Running())
        fmt.Printf("finish all tasks.
    ")
    
        //方法三:使用自定义数量的goroutinues
        // Use the pool with a function,
        // set 475 to the capacity of goroutine pool and 1 second for expired duration.
        p, _ := ants.NewPoolWithFunc(475, func(i interface{}) { // 并发数量475
            myFunc(i.(int32)) //业务函数
            wg.Done()
        }, ants.WithPreAlloc(true))
        defer p.Release()
        // Submit tasks one by one.
        for i := 0; i < runTimes; i++ {
            wg.Add(1)
            fmt.Printf("running goroutines: %d
    ", p.Running())
            _ = p.Invoke(int32(i)) // 传递业务函数的参数
        }
        wg.Wait()
    
        fmt.Printf("finish all tasks
    ")
    
        t := time.Since(t1)
        fmt.Println(t)
    }
  • 相关阅读:
    virtualbox+vagrant学习-2(command cli)-9-vagrant Plugin命令
    virtualbox+vagrant学习-2(command cli)-15-vagrant resume命令
    virtualbox+vagrant学习-2(command cli)-14-vagrant reload命令
    virtualbox+vagrant学习-2(command cli)-12-vagrant Provision命令
    virtualbox+vagrant学习-2(command cli)-11-vagrant PowerShell命令
    java设计模式之单例
    java设计模式之策略
    java设计模式之组合
    设计模式之适配器
    java设计模式之模板方法
  • 原文地址:https://www.cnblogs.com/wt11/p/14774822.html
Copyright © 2011-2022 走看看