funnext(): T? { while (true) { if (status == -1) { values.set(null) break } //可以提到循环外面 if (status == 0) { throw IllegalStateException("生成器未启动") } val c = mContinuation.getAndSet(null) c ?: continue
funmain(vararg args: String) { //声明生成器 var g = Generater.build { yield(0L) var i = 0L var j = 1L while (true) { yield(j) var next = i + j i = j j = next } } //多线程访问 Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start()
Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start()
Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start()
Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start()
Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start()
Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start()
Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start()
Thread { for (i in0..2) println(Thread.currentThread().name + ":" + g.next()) }.start() }