search

Home  >  Q&A  >  body text

java - 协程就是goto吗?

协程就是goto吗?两者有何异同?

高洛峰高洛峰2884 days ago1214

reply all(4)I'll reply

  • PHPz

    PHPz2017-04-17 15:28:00

    goto jumps within the same function. Coroutine should be similar to setjump and longjump, which jumps between different functions.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:28:00

    This is not accurate. It should be said that one of the main functions of coroutines is cross-function goto . Of course, goto can be returned when needed, which is more like a function call.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:28:00

    Coroutines are a component of computer programming that standardizes the concept of subroutines. Coroutines are very suitable for implementing some useful program components such as cooperative multitasking, exception handling, event loops, and iteration Devices, infinite linked lists and pipes, etc.

    The following compares general subroutines and coroutines:

    The beginning of a subroutine is the only entry point. Once exited, the execution of the subroutine is completed. An instance of the subroutine will only return once.

    A subroutine is always started at its beginning, which is usually a fixed position. A parallel program is always started at the next position where it last ended.

    Coroutines can call other coroutines through yield. The relationship between coroutines that transfer execution rights through yield is not the relationship between the caller and the callee, but is symmetrical and equal to each other.

    Subroutines are easy to implement on the stack because subroutines call other subroutines as subordinates. Instead, coroutines call other coroutines equally, best implemented using continuations (implemented by a garbage-collected heap) to track the flow of control.

    Marlin describes the characteristics of coroutine as follows:

    Local variables in the coroutine remain valid in subsequent calls.

    The coroutine is suspended when control leaves, and execution continues where it was suspended when control is returned to it.

    Classification of coroutines

    1) Classification by control transfer mechanism: symmetric coroutine and asymmetric coroutine

    Asymmetric coroutines (asymmetric), also known as semi-symmetric coroutines (semi-symmetric) or semi-coroutines (semi-coroutines). Asymmetric coroutines can be regarded as subordinate processes of the caller. The relationship between is similar to the relationship between calling and called routines.

    Symmetric coroutines only provide a control transfer mechanism: handing over the execution process to the specified coroutine. Because symmetric coroutines can transfer execution processes to each other, the relationship between them is as if they are at the same level.

    2) Is the coroutine a first-class type?

    3) Is there a complete stack?

    Coroutines with a complete stack allow the coroutine to suspend itself in the innermost function. After this coroutine is resumed, it will continue to execute from the place where it was suspended.

    Coroutines without a complete stack, such as Python’s generator, can only be suspended in the main body of the generator.

    Coroutines with a complete stack can be used to implement user-level multitasking, but coroutines without a complete stack cannot.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:28:00

    The keyword of coroutine is yield. Personally, I think the meaning should be:
    Interrupt execution and return data. Call again, return to breakpoint, and resume execution.
    yield is equivalent to return and goto breakpoint.

    reply
    0
  • Cancelreply