美文网首页
kotlin中channel

kotlin中channel

作者: ModestStorm | 来源:发表于2022-11-03 00:06 被阅读0次

channel用于协程之间的通讯,使用send和receive往通道里写入或者读取数据,2个方法为非阻塞挂起函数,channel是热流,不管有没有订阅者都会发送。

1.Channel的使用

 val channel = Channel<Int>()
  launch {
             channel.send(100)
        }


   launch {
            println("receive:" + channel.receive())

        }
打印 receive:100

Channel 默认的缓存容量buffer是0,send是个挂起函数,发送到通道,如果通道buffer已满,就会挂起send发送者函数。
如下所示:

val channel = Channel<Int>()
        launch {

            channel.send(100)
            println("sendbefore:200")
            channel.send(200)
            println("sendafter:200")
            channel.send(333)
        }


        launch {
            println("receive:" + channel.receive())

        }

打印
receive:100
sendbefore:200

channel.send(200) 发送到通道后,通道满了,发送者的协程send函数被挂起,所以 println("sendafter:200")不执行了。

recevie调用时,如果通道里不为空,就从通道里把元素拿出来,并且从通道里删除元素,如果通道元素为空,则挂起接收者协程即receive函数执行,等通道有元素后 才会执行。

例如:

    val channel = Channel<Int>()
        launch {

            delay(3000)
            channel.send(100)

        }


        launch {
            println("receive:before")
            println("receive:" + channel.receive())
            println("receive:after")
        }

输出:
receive:before
receive:100
receive:after
同时receive:before和 receive:100 间隔了3秒

channel的四种缓冲策略

Rendezvous: channel(默认类型): 0尺寸buffer,send是个挂起函数,发送到通道,如果通道buffer已满,就会挂起调用者,这个0buffer,发送一个,如果没人接收,调用者就被挂起

Buffered channel:指定元素大小,发送当buffer满了后Send会被挂起。

Conflated channel: 新元素会覆盖旧元素,receiver只会得到最新元素,Send永不挂起。

Unlimited channel: buffer无限,Send不被挂起。

使用方式:val rendezvousChannel = Channel<Int>(Channel.RENDEZVOUS)

相关文章

  • kotlin中channel

    channel用于协程之间的通讯,使用send和receive往通道里写入或者读取数据,2个方法为非阻塞挂起函数,...

  • # kotlin channel 入门

    kotlin channel 入门 前言 最近项目中对 kotlin 的使用比较多。不得不说 kotlin 确实...

  • Kotlin中Channel基本使用

    Channel的源代码如下: Channel的父类有发送消息的SendChannel和接受消息的ReceiveCh...

  • Kotlin Channel学习

    channel用于协程之间的通讯,使用send和receive往通道里写入或者读取数据,2个方法为非阻塞挂起函数,...

  • Kotlin语言(十二):Channel

    注:本文中使用 runBlocking 是为了方便测试,业务开发中禁止使用 一、Channel 基础 (1)Cha...

  • Flutter 开发 (3)Flutter 与 Native 的

    一、Flutter 中 Channel 基本概念 1. Flutter 中 Channel 的概念 Channel...

  • Kotlin协程Channel浅析

    结论先行 Kotlin协程中的Channel用于处理多个数据组合的流,随用随取,时刻准备着,就像自来水一样,打开开...

  • Java NIO Channel

    Channel(通道) 定义 Channel(通道)定义在java.nio.channels包中。Channel表...

  • nio杂记

    buffers只有btyebuffer能被channel使用 线程在channel中中断,会使当前channel关...

  • go for range 循环 和 channel

    输出: range 作用于 channel 时,会返回 channel 中所有的元素,如果 channel 中没有...

网友评论

      本文标题:kotlin中channel

      本文链接:https://www.haomeiwen.com/subject/fvustdtx.html