Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fiber.sleep Caused the loss of "tasks" #311

Open
yuri-li opened this issue Apr 30, 2018 · 6 comments
Open

Fiber.sleep Caused the loss of "tasks" #311

yuri-li opened this issue Apr 30, 2018 · 6 comments

Comments

@yuri-li
Copy link

yuri-li commented Apr 30, 2018

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
Copy link
Author

yuri-li commented Apr 30, 2018

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
Copy link
Author

yuri-li commented Apr 30, 2018

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
Copy link
Author

yuri-li commented Apr 30, 2018

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
Copy link
Author

yuri-li commented Apr 30, 2018

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
Copy link
Author

yuri-li commented Apr 30, 2018

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())
}

@doctorpangloss
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants