Re: Battles with CountedCompleter

Olivier Peyrusse via Concurrency-interest <[email protected]> Sun, 27 Sep 2020 14:28:40 +0000
Newsgroups gmane.comp.java.jsr.166-concurrency
Message-ID <_Kc7vfHVNaoSSQmi9-hg7TS62pfvpTSbArbpQxs7yxbKlrk_LsW6m6GQVsaPLh9-mS-Sqt2RiFCpEUSikxYbUEbohVA_O9xHSyHPBrasEC8=@protonmail.com>
Looking at the attached code, I don't see any obvious mistake.
I don't like the call to `complete` in `ManagedAction#compute`. I understand that your ManagedAction don't have children, so it may not cause harm, but I would prefer using the API "correctly", calling first `setRawResult` then `tryComplete`.

Can you share more details about the context when the error occurs? Or link to a gist reproducing your issue.
Do you have mutiple - nested - ForkJoinScopes? Your code depends on the number of cores, how many do you have? Are there exceptions?

Cheers
Olivier

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
Le vendredi 25 septembre 2020 02:01, Shevek via Concurrency-interest <[email protected]> a écrit :

> I have been doing battle with CountedCompleter, and I'm stuck at a point
> where I'm doing something like this:
>
> doInParallel(Iterable<thing> tasks) {
> CountedCompleter joinTask = new CountedCompleter();
> for (some unknown number of things)
> pool.submit(new CountedCompleter(parent, ...));
> joinTask.tryComplete();
> joinTask.join();
> }
>
> The objective is to have a recursively-safe construct like:
> try (ForkJoinScope scope = new ForkJoinScope(pool, ...)) {
> for (whatever)
> scope.execute(task);
> } // close() calls join()
>
> Any subtask may itself repeat this pattern. The trouble I'm having is
> that sometimes I get a lot of threads blocked here:
>
> "ForkJoinPool-1-worker-6" #19 daemon prio=5 os_prio=0
> tid=0x00007f39e5075800 nid=0xd0c in Object.wait() [0x00007f39515f7000]
> java.lang.Thread.State: WAITING (on object monitor)
> at java.lang.Object.wait(Native Method)
> at java.util.concurrent.ForkJoinTask.internalWait(ForkJoinTask.java:311)
> - locked <0x0000000477c78528> (a
> org.compilerworks.common.util.concurrent.ForkJoinScope$JoinTask)
> at java.util.concurrent.ForkJoinPool.awaitJoin(ForkJoinPool.java:2058)
> at
> java.util.concurrent.ForkJoinTask.doJoin(ForkJoinTask.java:390)
> at java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:719)
>
> What I can't work out is why these blocked threads aren't helping? 6 of
> the threads in my 12-thread pool are blocked, and 6 are working.
> Eventually, every so often, one of them seems to unblock. I'm trying to
> trace the logic in the code to work out why the blocked threads don't
> simply steal other work and do it. I've tried unit testing my
> wrapper/controller code twenty ways up and it doesn't block in tests,
> but it fails in application.
>
> Inspection of the heap of a blocked task shows (for example)
>
> -   joinTask.pending=32
> -   33 ForkJoinTask instances have a pointer to this joinTask as the
>     completer.
>
> -   joinTask and its children are in the correct ForkJoinPool
> -   Java 1.8.0_252
>
>     Can anybody please help? I'm happy to submit exact code as there are a
>     couple of nuances to what I'm doing which might be relevant.
>
>     I'm willing to be polite, but not effusive about the documentation of
>     CountedCompleter and similar, so it's entirely possible that I'm using
>     an API wrong.
>
>     Code is attached.
>
>     Thank you.
>
>     S.
>
>
> Concurrency-interest mailing list
> [email protected]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[email protected]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest