SynchronousQueue
Alan Bateman
Alan.Bateman at oracle.com
Sun Feb 10 09:53:18 UTC 2019
On 09/02/2019 12:33, Arkadiusz GasiĆski wrote:
> :
> I assumed (as SynchronousQueue has no capacity) that the output would be
> more like:
>
> Produder.offer
> Consumer.take
> Producer.offer
> Consumer.tak
>
> And so on. But in the actual output almost each time 2 tasks are actually
> offered and then 2 tasks are taken. Do you have any idea why this happens?
I briefly looked at your test and the producer and consumer (sharing one
thread) are running in lockstep, I think it's just the trace messages
that are a bit misleading. The producer prints the "Offering" message
before queue.put. The put doesn't need to block/park when the consumer
is parked/waiting for an element N so it will continue on and print
"Offering" message for element N+1 before it blocks/parks in queue.put.
On the consumer side, it prints "Processing" after queue.take has been
used to remove element N. The producer offering element N+1 has released
the thread to allow the consumer to continue so it will process element
N and continue to take element N+1. If you update the test to print a
trace message before and after both queue.put and queue.take then I
think it will be a bit clearer.
(On a side point, I regret using SQ in the demo as it's not really
suited for fibers at this time. It's really good for transfers between
threads where there is a lot of contention of course. TBD on what should
be done for fibers in this area).
-Alan
More information about the loom-dev
mailing list