0

I have a Class named Foo which calls a non-static method barMethod() of Bar. Barextends Fiber.
Inside barMethod() there is a call to park(). Now which Fiber will be parked? Foo instance or Bar instance?

Signature of park() (It is defined static and this is the main reason for my confusion):

public static void park()
                 throws SuspendExecution

If the answer is Foo (It seems to be so), How can I park Bar? I mainly intend to suspend (park) Bar not Foo.
And if you provide me an answer about how to park Barinstance, then please tell me that since I want to park Bar, not Foo, should barMethod() throw SuspendExecution? It will not be access by any instance of Bar, and I don't want to park any instance of Foo inside this method (only Bar).

Please also provide answer for all the same questions about unpark(). Will it be applied to current Fiber (Foo instance in this case) or it affects Bar instance? If latter, then how can I unpark Bar instance, not Foo instance?

Alireza Mohamadi
  • 751
  • 1
  • 6
  • 22

2 Answers2

0

Just like with threads, a fiber can only park itself. A call to park suspends the currently executing fiber. The class where the current method is defined is completely irrelevant. So the answer to your question of "which fiber will be parked, Bar or Foo", is "whichever fiber is currently executing". It can be the Bar instance, the Foo instance, or some other fiber altogether.

Just like with Thread, the only Fiber method that is ever worth overriding is run. Subclassing Fiber (or Thread) for any other reason makes no sense. It's best if you think of fibers and threads as being exactly the same idea, implemented in two different ways. A Thread is a thread implemented by the OS, and Fiber is a (lighweight) thread implemented in the JVM, but they both behave essentially the same.

Finally, park and unpark are a low-level API (just like LockSupport.park/unpark that do the same for plain threads), and unless you're writing a new concurrency mechanism (like a new kind of lock) you shouldn't use them directly. Instead, use any of the higher-level fiber synchronization APIs, like channels, locks etc..

pron
  • 1,693
  • 18
  • 28
-1

quasar discourages calling park and unpark directly

kilim is a similar tool that provides Fiber.yield (equivalent to park) and you can run a task directly. so i think it can do what you are looking for

in your example, barMethod should be marked as Pausable, but the calling method doesn't need to be

we recently did a pre-release of kilim 2.0. in particular, you should look at the Continuation class and the XorShift example

https://github.com/nqzero/kilim

nqzero
  • 125
  • 1
  • 7