quasar icon indicating copy to clipboard operation
quasar copied to clipboard

Fiber.sleep Caused the loss of "tasks"

Open yuri-li opened this issue 6 years ago • 6 comments

import co.paralleluniverse.fibers.Fiber

fun main(args: Array<String>) {
  Fiber<Void>{
        (1..3).forEach {
            println("current value: ${it}")
            Fiber.sleep(1000)
        }
    }.start()
}

please run it "gradle -q run",observe what the console prints Why only output 2? 3 was eaten?

yuri-li avatar Apr 30 '18 06:04 yuri-li

I don't know the reason, but the problem has been solved by changing the codding.

import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber
import java.util.concurrent.TimeUnit

fun main(args: Array<String>) {
    fiber @Suspendable {
        (1..3).forEach {
            Fiber.park({ println("current value: ${it}") }.invoke(), 1000, TimeUnit.MILLISECONDS)
            Fiber.currentFiber().unpark()
        }
    }
}

In addition, the language I use is kotlin

yuri-li avatar Apr 30 '18 08:04 yuri-li

import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber
import java.util.concurrent.TimeUnit

fun main(args: Array<String>) {
    fiber @Suspendable {
        (1..3).forEach {
            println("current value: ${it}")
            Fiber.sleep(1000)
        }
    }.get(4, TimeUnit.SECONDS)
}

yuri-li avatar Apr 30 '18 09:04 yuri-li

import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber

fun main(args: Array<String>) {
    fiber @Suspendable {
        (1..3).forEach {
            println("current value: ${it}")
            Fiber.sleep(1000)
        }
    }.join()
}

yuri-li avatar Apr 30 '18 09:04 yuri-li

If the above problem is solved, think about the following code:

import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber

fun main(args: Array<String>) {
    fiber @Suspendable {
        (1..3).forEach {
            println("fiber name f1, current value: ${it}")
            Fiber.sleep(1000)
        }
    }

    fiber @Suspendable {
        (1..3).forEach {
            println("fiber name f2, current value: ${it}")
            Fiber.sleep(1000)
        }
    }
}

yuri-li avatar Apr 30 '18 10:04 yuri-li

I know.

import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber

fun main(args: Array<String>) {
    val f1 = fiber @Suspendable {
        (1..3).forEach {
            println("fiber name f1, current value: ${it}")
            Fiber.sleep(1000)
        }
        "f1 all done"
    }

    val f2 = fiber @Suspendable {
        (1..3).forEach {
            println("fiber name f2, current value: ${it}")
            Fiber.sleep(1000)
        }
        "f2 all done"
    }
    println(f1.get())
    println(f2.get())
}

yuri-li avatar Apr 30 '18 10:04 yuri-li

I haven't run the code, so take this with a grain of salt (i.e, it may be totally wrong).

But I think the reason the first example exits is very simple. You reach the end of the main thread, and your application exits. The fiber isn't its own thread. If you created a new thread, then joined the fiber inside of it, and didn't join the new thread in your main method, everything would work.

You can see a similar issue here: https://stackoverflow.com/questions/52196187/what-happen-with-coroutines-when-main-thread-exits

doctorpangloss avatar Dec 06 '18 07:12 doctorpangloss