SoFunction
Updated on 2025-04-14

Implementation of thread pool in go language

Use goroutine and channel

There is no built-in concept directly similar to Java thread pool in the Go language, but it provides similar functions, mainly throughgoroutineandchannelto implement concurrent processing. You can implement the function of a "thread pool" by combining these two.

In Go,goroutineIt is a lightweight thread, managed by Go's scheduler, and can start concurrent tasks very easily. andchannelThis is used to pass messages or synchronize operations between different goroutines.

To implement a thread pool-like function, you can:

  • Create a fixed number of goroutines to handle tasks.
  • Pass tasks to these goroutines via channel.
  • Use a pool to manage the life cycle of goroutines.

Here is a simple example of simulating a fixed-size "thread pool":

package main

import (
	"fmt"
	"sync"
)

type Task struct {
	ID int
}

type WorkerPool struct {
	tasks   chan Task
	workers int
	wg      
}

func (wp *WorkerPool) Start() {
	for i := 0; i < ; i++ {
		go (i)
	}
}

func (wp *WorkerPool) worker(workerID int) {
	defer ()
	for task := range  {
		// Handle tasks		("Worker %d is processing task %d\n", workerID, )
	}
}

func (wp *WorkerPool) AddTask(task Task) {
	 <- task
}

func (wp *WorkerPool) Close() {
	close()
	()
}

func main() {
	// Create a pool with 3 workers	pool := WorkerPool{
		tasks:   make(chan Task, 10),
		workers: 3,
	}

	// Start worker	()

	// Add a task	for i := 1; i <= 5; i++ {
		(Task{ID: i})
	}

	// Close and wait for all tasks to complete	()
}

In this example:

  • WorkerPoolSimilar to a thread pool, managing multiple worker goroutines.
  • AddTaskUsed to add tasks to task queues (channels).
  • StartStart the worker goroutine to handle the task.
  • CloseUsed to close the task queue and wait for all tasks to complete.

In this way, you can control the number of concurrency, avoid creating too many goroutines, and effectively manage task execution.

If you want a more flexible thread pool implementation, there are also some third-party libraries in the Go community, such asants, it provides a mature thread pool implementation, supporting efficient resource management and task scheduling.
In addition to passing directlygoroutineandchannelTo implement thread pool-like functions, Go language has some other ways to implement thread pool concepts similar to Java. Common ways include:

use

is a memory pooling mechanism provided by Go, which is usually used for object multiplexing. Although it is not a thread pool in nature, it can be used to create a similar object pool, which can effectively reuse processed goroutines or task objects, thereby reducing the overhead of creating and destroying objects.

package main

import (
	"fmt"
	"sync"
)

type Task struct {
	ID int
}

func main() {
	var pool 

	// Initialize pool	 = func() interface{} {
		return &Task{}
	}

	// Get the object from pool	task := ().(*Task)
	 = 1
	("Processing task %d\n", )

	// Return the object to the pool	(task)
}

Mainly used to multiplex objects, so it can be reusedTaskObjects to reduce the burden of garbage collection, but it does not provide real concurrent task scheduling and execution capabilities. therefore,It is more suitable for managing object pools, rather than directly applicable to thread pool implementations.

Use third-party libraries (such as ants)

The Go community provides many mature third-party libraries to help implement concurrent task management similar to Java thread pools. A common library isants, it implements an efficient goroutine pool.

By usingants, you can implement concurrent execution of tasks and resource pool management, providing more functional and performance optimizations.

package main

import (
	"fmt"
	"/panjf2000/ants/v2"
)

func main() {
	// Create a thread pool that supports up to 10 concurrent tasks	pool, _ := (10)
	defer ()

	for i := 0; i < 20; i++ {
		task := i
		(func() {
			// Handle tasks			("Processing task %d\n", task)
		})
	}
}

In this example:

  • useCreate a thread pool of size 10 that can handle up to 10 tasks at the same time.
  • useSubmit tasks to the thread pool.
  • Tasks are executed by a work goroutine in the pool.

antsThe library provides thread pool management, including pool size, task scheduling and resource release functions, which is more convenient and efficient than using goroutine and channel directly.

Manage goroutines with custom scheduler

Another way is to customize a scheduler that limits the number of goroutines running simultaneously and avoids excessive consumption of system resources. For example, use a scheduler queue to manage the execution of tasks.

package main

import (
	"fmt"
	"sync"
	"time"
)

type Task struct {
	ID int
}

type Scheduler struct {
	taskQueue chan Task
	wg        
}

func NewScheduler(workerCount int) *Scheduler {
	return &Scheduler{
		taskQueue: make(chan Task),
	}
}

func (s *Scheduler) Start(workerCount int) {
	for i := 0; i < workerCount; i++ {
		go (i)
	}
}

func (s *Scheduler) worker(workerID int) {
	for task := range  {
		// Handle tasks		("Worker %d is processing task %d\n", workerID, )
		() // Simulate task execution time		()
	}
}

func (s *Scheduler) AddTask(task Task) {
	(1)
	 <- task
}

func (s *Scheduler) Close() {
	close()
	()
}

func main() {
	scheduler := NewScheduler(3)
	(3)

	// Submit task	for i := 1; i <= 10; i++ {
		(Task{ID: i})
	}

	// Wait for the task to be completed	()
}

In this implementation:

  • SchedulerusetaskQueueManage tasks, limiting the number of goroutines that handle tasks simultaneously.
  • workerWill come fromtaskQueueTake the task and process it.
  • AddTaskUsed to submit tasks,CloseUsed to close the task queue and wait for all tasks to complete.

This method allows you to customize more scheduling strategies, control task execution and goroutine management.

Summarize

The Go language itself does not have a direct concept similar to Java thread pool, but you can use the following ways to implement similar functions:

  • Manually implement thread pooling through goroutine and channel.
  • useManage object pools.
  • Use community library such asantsTo efficiently manage goroutine pools.
  • Custom scheduler to limit the number of concurrent tasks.

Depending on your needs, choose the right way to implement concurrent tasks.

This is the end of this article about the implementation of thread pools in the go language. For more related go languages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!