最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

golang實(shí)現(xiàn)協(xié)程池的方法示例

 更新時(shí)間:2025年02月06日 09:34:05   作者:陳墨1234  
本文主要介紹了golang實(shí)現(xiàn)協(xié)程池的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

go協(xié)程池可以看成一個(gè)被初始化的固定大小的協(xié)程循環(huán)讀取函數(shù)隊(duì)列,獲取是否有可供調(diào)用的函數(shù)隊(duì)列,如果有,則協(xié)程池中的一個(gè)協(xié)程調(diào)用并執(zhí)行該函數(shù),talk is cheap,show me the code。

主體協(xié)程池代碼如下所示:

package goroutine

import (
	"context"
	"fmt"
	"sync"
)

// go實(shí)現(xiàn)簡(jiǎn)單協(xié)程池
type Pool struct {
	ctx   context.Context
	tasks []fn
	lock  sync.Locker
}

type fn func()

func Init(ctx context.Context, cnt int) *Pool {
	pool := &Pool{
		ctx:   ctx,
		tasks: make([]fn, 0),
		lock:  NewSpinLock(), //自定義自旋鎖實(shí)現(xiàn),如果使用go自帶的鎖,則可能會(huì)出現(xiàn)死鎖問(wèn)題
	}
	for i := 0; i < cnt; i++ {
		go func(pool *Pool, idx int) {
			for {
				select {
				case <-pool.ctx.Done():
					fmt.Println("pool exit", idx)
					return
				default:
					fmt.Println("pool exec:", idx)
					pool.lock.Lock()
					if len(pool.tasks) == 0 {
						pool.lock.Unlock()
						continue
					}
					fc := pool.tasks[0]
					pool.tasks = pool.tasks[1:]
					pool.lock.Unlock()
					fc()
				}
			}

		}(pool, i)
	}
	return pool
}

func (p *Pool) Put(fc fn) {
	p.lock.Lock()
	p.tasks = append(p.tasks, fc)
	p.lock.Unlock()
}

自定義的自旋鎖實(shí)現(xiàn)代碼如下:

package goroutine

import (
	"runtime"
	"sync"
	"sync/atomic"
)

type spinLock uint32

const maxBackoff = 16

func (sl *spinLock) Lock() {
	backoff := 1
	for !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {
		// Leverage the exponential backoff algorithm, see https://en.wikipedia.org/wiki/Exponential_backoff.
		for i := 0; i < backoff; i++ {
			runtime.Gosched()
		}
		if backoff < maxBackoff {
			backoff <<= 1
		}
	}
}

func (sl *spinLock) Unlock() {
	atomic.StoreUint32((*uint32)(sl), 0)
}

// NewSpinLock instantiates a spin-lock.
func NewSpinLock() sync.Locker {
	return new(spinLock)
}

測(cè)試用的代碼如下所示

package goroutine

import (
	"context"
	"fmt"
	"log"
	"strconv"
	"sync"
	"testing"
	"time"
)

func TestPool(t *testing.T) {
	ctx := context.Background()
	ctx1, cancel := context.WithCancel(ctx)
	pool := Init(ctx1, 10)
	wg := sync.WaitGroup{}
	for i := 100; i >= 0; i-- {
		wg.Add(1)
		go pool.Put(func() {
			wg.Done()
			log.Println("hello,world" + strconv.Itoa(i))
		})
	}
	wg.Wait()
	cancel()
	time.Sleep(time.Second)
}

開(kāi)發(fā)環(huán)境為goland,運(yùn)行結(jié)果截圖如下圖:

=== RUN   TestPool
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 0
pool exec: 2
pool exec: 0
pool exec: 1
pool exec: 3
pool exec: 4
pool exec: 7
pool exec: 8
pool exec: 5
pool exec: 6
pool exec: 9
2024/08/02 23:06:20 hello,world97
pool exec: 2
2024/08/02 23:06:20 hello,world42
pool exec: 2
2024/08/02 23:06:20 hello,world57
pool exec: 2
2024/08/02 23:06:20 hello,world56
pool exec: 2
2024/08/02 23:06:20 hello,world55
pool exec: 2
2024/08/02 23:06:20 hello,world54
pool exec: 2
2024/08/02 23:06:20 hello,world53
pool exec: 2
2024/08/02 23:06:20 hello,world52
pool exec: 2
2024/08/02 23:06:20 hello,world51
pool exec: 2
2024/08/02 23:06:20 hello,world50
pool exec: 2
2024/08/02 23:06:20 hello,world49
pool exec: 2
2024/08/02 23:06:20 hello,world48
pool exec: 2
2024/08/02 23:06:20 hello,world47
pool exec: 2
2024/08/02 23:06:20 hello,world46
pool exec: 2
2024/08/02 23:06:20 hello,world45
pool exec: 2
2024/08/02 23:06:20 hello,world44
pool exec: 2
2024/08/02 23:06:20 hello,world95
pool exec: 0
2024/08/02 23:06:20 hello,world43
pool exec: 2
2024/08/02 23:06:20 hello,world41
pool exec: 2
2024/08/02 23:06:20 hello,world40
pool exec: 2
2024/08/02 23:06:20 hello,world33
pool exec: 0
2024/08/02 23:06:20 hello,world39
pool exec: 2
2024/08/02 23:06:20 hello,world38
pool exec: 0
2024/08/02 23:06:20 hello,world37
2024/08/02 23:06:20 hello,world36
pool exec: 2
pool exec: 0
2024/08/02 23:06:20 hello,world96
pool exec: 4
2024/08/02 23:06:20 hello,world98
2024/08/02 23:06:20 hello,world99
pool exec: 3
2024/08/02 23:06:20 hello,world94
2024/08/02 23:06:20 hello,world32
pool exec: 7
2024/08/02 23:06:20 hello,world35
2024/08/02 23:06:20 hello,world31
pool exec: 7
2024/08/02 23:06:20 hello,world90
pool exec: 9
pool exec: 3
2024/08/02 23:06:20 hello,world28
pool exec: 2
2024/08/02 23:06:20 hello,world29
pool exec: 1
2024/08/02 23:06:20 hello,world24
2024/08/02 23:06:20 hello,world26
pool exec: 2
2024/08/02 23:06:20 hello,world34
pool exec: 1
2024/08/02 23:06:20 hello,world93
pool exec: 9
2024/08/02 23:06:20 hello,world21
pool exec: 8
2024/08/02 23:06:20 hello,world92
pool exec: 1
2024/08/02 23:06:20 hello,world23
pool exec: 5
2024/08/02 23:06:20 hello,world22
pool exec: 3
2024/08/02 23:06:20 hello,world20
pool exec: 8
2024/08/02 23:06:20 hello,world25
pool exec: 1
2024/08/02 23:06:20 hello,world91
2024/08/02 23:06:20 hello,world89
pool exec: 6
2024/08/02 23:06:20 hello,world88
pool exec: 6
2024/08/02 23:06:20 hello,world66
pool exec: 6
2024/08/02 23:06:20 hello,world87
pool exec: 9
2024/08/02 23:06:20 hello,world27
pool exec: 4
2024/08/02 23:06:20 hello,world73
pool exec: 4
2024/08/02 23:06:20 hello,world18
pool exec: 1
2024/08/02 23:06:20 hello,world72
pool exec: 6
pool exec: 9
2024/08/02 23:06:20 hello,world30
pool exec: 5
pool exec: 0
2024/08/02 23:06:20 hello,world81
pool exec: 7
2024/08/02 23:06:20 hello,world68
pool exec: 2
2024/08/02 23:06:20 hello,world74
2024/08/02 23:06:20 hello,world67
pool exec: 3
pool exec: 0
2024/08/02 23:06:20 hello,world79
2024/08/02 23:06:20 hello,world85
2024/08/02 23:06:20 hello,world19
pool exec: 3
2024/08/02 23:06:20 hello,world86
pool exec: 4
2024/08/02 23:06:20 hello,world71
2024/08/02 23:06:20 hello,world77
pool exec: 3
2024/08/02 23:06:20 hello,world84
pool exec: 0
2024/08/02 23:06:20 hello,world83
pool exec: 8
pool exec: 9
2024/08/02 23:06:20 hello,world78
pool exec: 4
2024/08/02 23:06:20 hello,world69
pool exec: 2
pool exec: 6
2024/08/02 23:06:20 hello,world76
pool exec: 9
pool exec: 4
2024/08/02 23:06:20 hello,world63
pool exec: 9
2024/08/02 23:06:20 hello,world65
pool exec: 8
2024/08/02 23:06:20 hello,world60
pool exec: 4
2024/08/02 23:06:20 hello,world80
pool exec: 7
2024/08/02 23:06:20 hello,world59
pool exec: 4
2024/08/02 23:06:20 hello,world13
pool exec: 9
2024/08/02 23:06:20 hello,world75
2024/08/02 23:06:20 hello,world16
pool exec: 0
2024/08/02 23:06:20 hello,world15
pool exec: 0
2024/08/02 23:06:20 hello,world14
pool exec: 0
2024/08/02 23:06:20 hello,world11
pool exec: 0
2024/08/02 23:06:20 hello,world58
pool exec: 1
2024/08/02 23:06:20 hello,world82
pool exec: 9
pool exec: 5
2024/08/02 23:06:20 hello,world10
pool exec: 9
2024/08/02 23:06:20 hello,world0
pool exec: 7
pool exec: 0
2024/08/02 23:06:20 hello,world8
pool exec: 7
2024/08/02 23:06:20 hello,world7
2024/08/02 23:06:20 hello,world62
pool exec: 9
pool exec: 2
2024/08/02 23:06:20 hello,world9
pool exec: 2
2024/08/02 23:06:20 hello,world2
pool exec: 2
2024/08/02 23:06:20 hello,world4
pool exec: 9
2024/08/02 23:06:20 hello,world3
pool exec: 9
2024/08/02 23:06:20 hello,world100
pool exec: 9
pool exec: 9
pool exec: 9
2024/08/02 23:06:20 hello,world5
pool exec: 9
pool exec: 9
2024/08/02 23:06:20 hello,world1
pool exec: 9
2024/08/02 23:06:20 hello,world64
pool exec: 0
2024/08/02 23:06:20 hello,world61
pool exit 8
2024/08/02 23:06:20 hello,world70
pool exec: 2
pool exit 2
2024/08/02 23:06:20 hello,world17
pool exit 0
2024/08/02 23:06:20 hello,world12
pool exit 4
2024/08/02 23:06:20 hello,world6
pool exit 7
pool exit 3
pool exec: 9
pool exit 9
pool exit 1
pool exec: 5
pool exit 5
pool exit 6
--- PASS: TestPool (1.00s)
PASS

Process finished with the exit code 0

好了,整體代碼介紹完了,希望你能對(duì)協(xié)程池有個(gè)比較簡(jiǎn)單的了解,也可以基于此代碼,豐富一下邏輯

到此這篇關(guān)于golang實(shí)現(xiàn)協(xié)程池的方法示例的文章就介紹到這了,更多相關(guān)golang 協(xié)程池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go語(yǔ)言中g(shù)PRC的使用

    Go語(yǔ)言中g(shù)PRC的使用

    本文主要介紹了Go語(yǔ)言中g(shù)PRC的使用,包括Protobuf定義服務(wù)接口、HTTP/2協(xié)議與性能優(yōu)勢(shì),以及流模式和發(fā)布-訂閱系統(tǒng)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-07-07
  • 淺談Go語(yǔ)言多態(tài)的實(shí)現(xiàn)與interface使用

    淺談Go語(yǔ)言多態(tài)的實(shí)現(xiàn)與interface使用

    如果大家系統(tǒng)的學(xué)過(guò)C++、Java等語(yǔ)言以及面向?qū)ο蟮脑?,相信?yīng)該對(duì)多態(tài)不會(huì)陌生。多態(tài)是面向?qū)ο蠓懂牣?dāng)中經(jīng)常使用并且非常好用的一個(gè)功能,它主要是用在強(qiáng)類型語(yǔ)言當(dāng)中,像是Python這樣的弱類型語(yǔ)言,變量的類型可以隨意變化,也沒(méi)有任何限制,其實(shí)區(qū)別不是很大
    2021-06-06
  • 使用Go語(yǔ)言編寫(xiě)一個(gè)極簡(jiǎn)版的容器Container

    使用Go語(yǔ)言編寫(xiě)一個(gè)極簡(jiǎn)版的容器Container

    Docker作為一種流行的容器化技術(shù),對(duì)于每一個(gè)程序開(kāi)發(fā)者而言都具有重要性和必要性,因?yàn)槿萜骰嚓P(guān)技術(shù)的普及大大簡(jiǎn)化了開(kāi)發(fā)環(huán)境配置、更好的隔離性和更高的安全性,對(duì)于部署項(xiàng)目和團(tuán)隊(duì)協(xié)作而言也更加方便,本文將嘗試使用Go語(yǔ)言編寫(xiě)一個(gè)極簡(jiǎn)版的容器
    2023-10-10
  • goland實(shí)現(xiàn)自動(dòng)格式化代碼

    goland實(shí)現(xiàn)自動(dòng)格式化代碼

    本文介紹了三種在GoLand中自動(dòng)格式化Go代碼的方法:使用FileWatchers在編碼過(guò)程中自動(dòng)格式化、利用Git提交工具在提交前自動(dòng)格式化以及使用快捷鍵手動(dòng)格式化,每種方法都有其特點(diǎn)和適用場(chǎng)景
    2025-12-12
  • Golang中時(shí)間格式化的實(shí)現(xiàn)詳解

    Golang中時(shí)間格式化的實(shí)現(xiàn)詳解

    這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言中進(jìn)行時(shí)間進(jìn)行格式化的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-09-09
  • Golang開(kāi)發(fā)之接口的具體使用詳解

    Golang開(kāi)發(fā)之接口的具體使用詳解

    在 Golang 中,接口是一種類型,它是由一組方法簽名組成的抽象集合。這篇文章主要為大家介紹了Golang接口的具體使用,希望對(duì)大家有所幫助
    2023-04-04
  • golang DNS服務(wù)器的簡(jiǎn)單實(shí)現(xiàn)操作

    golang DNS服務(wù)器的簡(jiǎn)單實(shí)現(xiàn)操作

    這篇文章主要介紹了golang DNS服務(wù)器的簡(jiǎn)單實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • 如何用go操作iptables和ipset設(shè)置黑白名單

    如何用go操作iptables和ipset設(shè)置黑白名單

    這篇文章主要介紹了如何用go操作iptables和ipset設(shè)置黑白名單問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • Golang中指針的使用詳解

    Golang中指針的使用詳解

    Golang是一門支持指針的編程語(yǔ)言,指針是一種特殊的變量,存儲(chǔ)了其他變量的地址。通過(guò)指針,可以在程序中直接訪問(wèn)和修改變量的值,避免了不必要的內(nèi)存拷貝和傳遞。Golang中的指針具有高效、安全的特點(diǎn),在并發(fā)編程和底層系統(tǒng)開(kāi)發(fā)中得到廣泛應(yīng)用
    2023-04-04
  • golang配制高性能sql.DB的使用

    golang配制高性能sql.DB的使用

    本文主要講述SetMaxOpenConns(),?SetMaxIdleConns()?和?SetConnMaxLifetime()方法,?您可以使用它們來(lái)配置sql.DB的行為并改變其性能,感興趣的可以了解一下
    2021-12-12

最新評(píng)論

大足县| 泉州市| 凤庆县| 内江市| 宁城县| 瑞丽市| 平阴县| 闸北区| 冷水江市| 渝北区| 祁阳县| 乐平市| 准格尔旗| 玛沁县| 临湘市| 尚志市| 浪卡子县| 加查县| 洪湖市| 石林| 桂林市| 紫金县| 右玉县| 临洮县| 洛川县| 北票市| 永兴县| 墨玉县| 德州市| 彝良县| 滦平县| 庆元县| 蒙自县| 富川| 遂宁市| 玛沁县| 三门县| 白玉县| 汽车| 梅河口市| 宁国市|