SynchronousQueue
Arkadiusz Gasiński
jigga at jigga.pl
Sat Feb 9 12:33:54 UTC 2019
Hi,
I watched Alan's Loom presentation from last year's Devoxx and there was
this SynchronousQueue example <https://youtu.be/vbGbXUjlRyQ?t=689>. I
decided to try it out with the latest loom build and actually one thing
caught me by surprise.
Here are the sources
<https://github.com/jigga/loom-workshop/blob/master/synchronous-queue/src/main/java/loom/SyncQueueDemo.java>
of mine rendition of Alan's example.
If you want to quickly run it, you can basically do the following:
docker run -it gasinski/debian-loom-workshop
root at 65a43986c5b0:/loom-workshop# ./gradlew :synchronous-queue:bootRun
What surprised me was the output, which always looks like this:
> Task :synchronous-queue:bootRun
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 1, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 1, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 2, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 3, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 2, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 3, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 4, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 5, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 4, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 5, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 6, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 7, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 6, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 7, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 8, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Offering a task: {"id" : 9, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 8, "class" : "Task"}
Thread[Fiber,5,ShadowThreads]|Processing task: {"id" : 9, "class" : "Task"}
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?
Thanks,
Arek
More information about the loom-dev
mailing list