Home  >  Article  >  Backend Development  >  Implementing multi-thread and multi-coroutine task coordination through Channels in Golang

Implementing multi-thread and multi-coroutine task coordination through Channels in Golang

WBOY
WBOYOriginal
2023-08-08 14:13:061414browse

Golang 中通过 Channels 实现多线程和多协程的任务协同

Use Channels to achieve multi-thread and multi-coroutine task coordination in Golang

Overview:

In Golang, it is very convenient to use Channels Easily realize task collaboration between multi-threads and multi-coroutines. Channels act as a bridge for communication between threads and can be used to send and receive data. Through Channels, we can realize data sharing and synchronization between multi-threads and multi-coroutines, thereby achieving collaborative processing of tasks.

Code example:

package main

import (
    "fmt"
    "time"
)

func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        fmt.Println("worker", id, "started job", j)
        time.Sleep(time.Second) // 模拟进行任务处理
        fmt.Println("worker", id, "finished job", j)
        results <- j * 2
    }
}

func main() {
    numJobs := 5
    jobs := make(chan int, numJobs)
    results := make(chan int, numJobs)

    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    for j := 1; j <= numJobs; j++ {
        jobs <- j
    }
    close(jobs)

    for a := 1; a <= numJobs; a++ {
        <-results
    }
}

Analysis:

In the above code, we created the worker function to simulate a coroutine that processes tasks. This function receives tasks from the jobs channel and sends the processing results to the results channel.

In the main function, we create a jobs channel and a results channel and pass them to each worker coroutine respectively. We then use a loop to send tasks to the jobs channel, and close the channel with close to indicate that all tasks have been sent.

Finally, we use a loop to receive the processing results from the results channel. Since the buffer size of the results channel is equal to the number of tasks, it is guaranteed that all task processing results are received.

Run this code, the output result is as follows:

worker 1 started job 1
worker 2 started job 2
worker 3 started job 3
worker 1 finished job 1
worker 1 started job 4
worker 2 finished job 2
worker 2 started job 5
worker 3 finished job 3
worker 1 finished job 4
worker 2 finished job 5

As you can see from the output result, the three worker coroutines start executing tasks at the same time, and after completing the task, the results are sent to the results channel middle. Since the buffer size of the results channel is the number of tasks, it is guaranteed that the results of all tasks can be received.

Summary:

Through Channels in Golang, we can easily achieve task collaboration between multi-threads and multi-coroutines. By using Channels, we can easily communicate and share data between threads, thereby improving program concurrency and efficiency.

Through the code examples in this article, I hope readers can have a deeper understanding of the principles and methods of achieving multi-threaded and multi-coroutine task coordination through Channels in Golang. At the same time, we also hope that readers can make full use of the features of Channels in actual development and take advantage of Golang's concurrent programming.

The above is the detailed content of Implementing multi-thread and multi-coroutine task coordination through Channels in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn