golang并发模式work

本文主要探讨了Golang中的并发模式,通过分析`work.go`和`main.go`两个文件的代码,展示了如何在Go语言中利用goroutines和channels实现高效的并发处理。`work.go`包含了工作逻辑,而`main.go`则是程序的入口,调用了并发任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 work.go

package work

import (
"sync"
)
type Worker interface {  //定义接口,需实现Task()函数
Task()
}
type Pool struct {          
work chan Worker    //通道,发送worker
wg sync.WaitGroup   
}
func New(maxGoroutines int) *Pool {
p := Pool {
work :make(chan Worker),
}
p.wg.Add(maxGoroutines)
for i := 0; i< maxGoroutines; i++ {
go func(){
for w := range p.work {  //这种方式从一个chan中取东西&
### Golang 并发编程练习示例代码 #### 使用 `sync.WaitGroup` 实现并发任务等待完成 下面展示了一个使用 `sync.WaitGroup` 的简单例子,该程序启动多个 Goroutine 来模拟并发任务并等待它们全部完成。 ```go package main import ( "fmt" "sync" ) func worker(id int, wg *sync.WaitGroup) { defer wg.Done() fmt.Printf("Worker %d starting\n", id) // Simulate some work with a sleep. for i := 0; i < 3; i++ { fmt.Printf("Worker %d is working...\n", id) } fmt.Printf("Worker %d done\n", id) } func main() { var wg sync.WaitGroup numWorkers := 5 for i := 1; i <= numWorkers; i++ { wg.Add(1) go worker(i, &wg) } wg.Wait() fmt.Println("All workers have finished their tasks.") } ``` 此代码展示了如何通过 `WaitGroup` 控制多个 Goroutine 的生命周期[^4]。 --- #### 使用 Channel 进行消息传递的任务队列改造 基于引用中的计时器退出逻辑[^3],可以将其改造成一个简单的任务队列模型。以下是一个改进版本: ```go package main import ( "fmt" "time" ) type Task struct { Name string Duration time.Duration } var taskQueue chan Task var exitChan chan bool func processTasks(ticker *time.Ticker) { for { select { case t := <-taskQueue: fmt.Printf("Processing task: %s\n", t.Name) time.Sleep(t.Duration) fmt.Printf("Task completed: %s\n", t.Name) case <-ticker.C: fmt.Println("Checking for new tasks...") case <-exitChan: fmt.Println("Exiting the processor goroutine.") return } } } func main() { taskQueue = make(chan Task, 10) exitChan = make(chan bool, 1) ticker := time.NewTicker(time.Second * 2) go processTasks(ticker) // Add tasks to the queue for i := 1; i <= 5; i++ { task := Task{Name: fmt.Sprintf("Task-%d", i), Duration: time.Second} taskQueue <- task if i == 3 { close(taskQueue) // Close channel after adding all tasks (optional). } } // Exit signal after processing all tasks time.Sleep(time.Second * 8) exitChan <- true } ``` 上述代码创建了一个任务队列,并利用通道来管理任务的分发和处理过程。 --- #### 使用 Select 和 Timeout 处理超时场景 以下代码演示了如何在 Go 中使用 `select` 和 `time.After` 函数实现超时控制[^2]。 ```go package main import ( "fmt" "time" ) func fetchData(ch chan<- string) { time.Sleep(3 * time.Second) // Simulating network delay or computation ch <- "Data fetched successfully!" } func main() { dataCh := make(chan string, 1) timeoutCh := time.After(2 * time.Second) go fetchData(dataCh) select { case data := <-dataCh: fmt.Println("Received:", data) case <-timeoutCh: fmt.Println("Request timed out!") } } ``` 在此示例中,如果数据获取超过指定的时间,则会触发超时机制。 --- #### 使用 Mutex 和 Condition 变量同步访问共享资源 以下代码展示了如何使用 `sync.Mutex` 或 `sync.Cond` 同步对共享变量的访问。 ```go package main import ( "fmt" "sync" ) func main() { var mu sync.Mutex count := 0 increment := func(wg *sync.WaitGroup) { mu.Lock() defer mu.Unlock() count++ fmt.Println("Count incremented:", count) wg.Done() } var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go increment(&wg) } wg.Wait() fmt.Println("Final Count:", count) } ``` 这段代码说明了如何防止竞争条件的发生,从而确保线程安全的操作。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值