go之channel任意任务完成、全部任务完成退出

简介: go之channel任意任务完成、全部任务完成退出

一、任意任务完成

import (
  "fmt"
  "runtime"
  "testing"
  "time"
)
 
func runTask(id int) string {
  time.Sleep(time.Millisecond * 10)
  return fmt.Sprintf("The result is from %d", id)
}
 
//任意任务完成
func FirstReponse() string {
  numOfRunner := 10
  //buffer chan防止协程泄露
  ch := make(chan string, numOfRunner)
  for i := 0; i < numOfRunner; i++ {
    go func(i int) {
      ret := runTask(i)
      ch <- ret
    }(i)
  }
  //任意任务完成
  return <-ch
}

二、全部任务完成

import (
  "fmt"
  "runtime"
  "testing"
  "time"
)
 
func runTask(id int) string {
  time.Sleep(time.Millisecond * 10)
  return fmt.Sprintf("The result is from %d", id)
}
 
//全部任务完成后返回
func AllReponse() string {
  numOfRunner := 10
  //buffer chan防止协程泄露
  ch := make(chan string, numOfRunner)
  for i := 0; i < numOfRunner; i++ {
    go func(i int) {
      ret := runTask(i)
      ch <- ret
    }(i)
  }
  finalRet := ""
  for n := 0; n < numOfRunner; n++ {
    finalRet += <-ch + "\n"
  }
  return finalRet
}

三、测试

import (
  "fmt"
  "runtime"
  "testing"
  "time"
)
 
func runTask(id int) string {
  time.Sleep(time.Millisecond * 10)
  return fmt.Sprintf("The result is from %d", id)
}
 
//任意任务完成
func FirstReponse() string {
  numOfRunner := 10
  //buffer chan防止协程泄露
  ch := make(chan string, numOfRunner)
  for i := 0; i < numOfRunner; i++ {
    go func(i int) {
      ret := runTask(i)
      ch <- ret
    }(i)
  }
  //任意任务完成
  return <-ch
}
 
//全部任务完成后返回
func AllReponse() string {
  numOfRunner := 10
  //buffer chan防止协程泄露
  ch := make(chan string, numOfRunner)
  for i := 0; i < numOfRunner; i++ {
    go func(i int) {
      ret := runTask(i)
      ch <- ret
    }(i)
  }
  finalRet := ""
  for n := 0; n < numOfRunner; n++ {
    finalRet += <-ch + "\n"
  }
  return finalRet
}
func TestFirstResponse(t *testing.T) {
  t.Log("Before:", runtime.NumGoroutine())
  //t.Log(FirstReponse())
  t.Log(AllReponse())
  time.Sleep(time.Second * 1)
  t.Log("After:", runtime.NumGoroutine())
}
任意任务完成
=== RUN   TestFirstResponse
    first_response_test.go:48: Before: 2
    first_response_test.go:49: The result is from 0
    first_response_test.go:52: After: 2
--- PASS: TestFirstResponse (1.03s)
PASS
 
全部任务完成
=== RUN   TestFirstResponse
    first_response_test.go:48: Before: 2
    first_response_test.go:50: The result is from 4
        The result is from 6
        The result is from 9
        The result is from 5
        The result is from 1
        The result is from 7
        The result is from 8
        The result is from 3
        The result is from 0
        The result is from 2
        
    first_response_test.go:52: After: 2
--- PASS: TestFirstResponse (1.03s)
PASS
目录
相关文章
|
1月前
|
存储 安全 Java
Go 基础数据结构的底层原理(slice,channel,map)
Go 基础数据结构的底层原理(slice,channel,map)
65 0
|
4天前
|
Go
go之channel关闭与广播
go之channel关闭与广播
6 0
|
10天前
|
存储 Go
Go 语言当中 CHANNEL 缓冲
Go 语言当中 CHANNEL 缓冲
|
22天前
|
并行计算 Go 数据处理
掌握Go语言:Go 并发编程,轻松应对大规模任务处理和高并发请求(34)
掌握Go语言:Go 并发编程,轻松应对大规模任务处理和高并发请求(34)
|
1月前
|
负载均衡 Go 调度
使用Go语言构建高性能的Web服务器:协程与Channel的深度解析
在追求高性能Web服务的今天,Go语言以其强大的并发性能和简洁的语法赢得了开发者的青睐。本文将深入探讨Go语言在构建高性能Web服务器方面的应用,特别是协程(goroutine)和通道(channel)这两个核心概念。我们将通过示例代码,展示如何利用协程处理并发请求,并通过通道实现协程间的通信和同步,从而构建出高效、稳定的Web服务器。
|
1月前
|
设计模式 缓存 安全
一篇文章带你吃透Go语言的Atomic和Channel--实战方法
一篇文章带你吃透Go语言的Atomic和Channel--实战方法
45 0
|
1月前
|
存储 Go
|
1月前
|
安全 Go 调度
Go语言的并发编程:goroutine和channel详解
Go语言的并发编程:goroutine和channel详解
127 2
|
1月前
|
NoSQL Go Redis
Go异步任务处理解决方案:Asynq
Go异步任务处理解决方案:Asynq
226 1
Go异步任务处理解决方案:Asynq
|
1月前
|
Go
Go语言Channel进阶:巧妙运用超时机制
Go语言Channel进阶:巧妙运用超时机制
170 0