我在 Kotlin 中使用 Quasar 并开始使用主题和频道。我正在关注一些 Java 示例并编写了一个小片段。不幸的是,我似乎无法让它工作。它只是阻塞主线程并等待。我不太确定 join 正在做什么,想知道这是否是问题所在?
public fun channelsDemo() {
val x: Val<Int> = Val()
val t: Topic<Int> = Topic()
fiber {
val c: Channel<Int> = t.subscribe(Channels.newChannel(0))
do {
val m = c.receive()
System.out.println("Channel => " + (m + x.get()))
} while (m != null)
}.join()
x.set(13)
t.send(5)
t.close()
}
编辑:
public fun channelsDemo() {
val x: Val<Int> = Val()
val t: Topic<Int> = Topic()
fiber {
val c: Channel<Int> = t.subscribe(Channels.newChannel(0))
do {
val m = c.receive()
System.out.println("Channel => " + (m + x.get()))
} while (m != 40)
}
fiber {
Strand.sleep(20000)
x.set(13)
t.send(5)
t.send(21)
t.send(40)
t.close()
}
}