11.4 使用select进行调度

一、调度器

  1. select调度器可以进行调度,对由内容的管道数据进行处理

  2. select中可是使用Nil Channel(数据没有准备好的时候)

  3. select与case配队使用,也可以使用default,但是不建议

    select {
    	case n = <-c1:
    		hasValue = true
    	case n = <-c2:
    		hasValue = true
    	case activateWorker <- n:
    		hasValue = false
    	case <-tm:   // 定时
    		fmt.Printf("结束运行")
    		return
    	// default:
    	// 	fmt.Println("没有数据到达")
    }
  4. 定时

    // 计时器
    tm := time.After(10 * time.Second)

    备注:定时器返回的内容是通过一个管道返回的,因此可以通过2中的调度去调度

  5. 可以进行系统的相关超时处理等

Last updated

Was this helpful?