Re: Better RecursiveTask Example

"Dr Heinz M. Kabutz via Concurrency-interest" <[email protected]> Thu, 25 Nov 2021 08:03:11 +0200
Newsgroups gmane.comp.java.jsr.166-concurrency
Organization JavaSpecialists.eu
Message-ID <[email protected]>
--===============8613832391509857264==
Content-Type: text/html; charset=utf-8
Content-Language: en-US
Content-Transfer-Encoding: 8bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <pre class="moz-quote-pre" wrap="">The demo code did not compile, because the fork() method returns a ForkJoinTask&lt;BigInteger&gt;. We can either change the types to ForkJoinTask&lt;BigInteger&gt; or alternatively do the fork() as a separate step, as in my original example.

 *
 * &lt;pre&gt; {@code
 * public class FactorialTask extends RecursiveTask&lt;BigInteger&gt; {
 *   private final int from;
 *   private final int to;
 *
 *   public FactorialTask(int n) {
 *     this(0, n);
 *   }
 *
 *   private FactorialTask(int from, int to) {
 *     this.from = from;
 *     this.to = to;
 *   }
 *
 *   protected BigInteger compute() {
 *     if (from == to) {                      // base case
 *       return (from == 0) ? BigInteger.ONE : BigInteger.valueOf(from);
 *     }
 *     int mid = (from + to) &gt;&gt;&gt; 1;           // split in half
 *     ForkJoinTask&lt;BigInteger&gt; leftTask = new FactorialTask(from, mid).fork();
 *     ForkJoinTask&lt;BigInteger&gt;
 rightTask = new FactorialTask(mid + 1, to);
 *     BigInteger right = rightTask.invoke(); // perform half the work locally
 *     BigInteger left = leftTask.join();
 *     return left.multiply(right);
 *   }
 * }&lt;/pre&gt;
 *
</pre>
    <pre class="moz-signature" cols="72">Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java™ Specialists' Newsletter" - <a class="moz-txt-link-abbreviated" href="http://www.javaspecialists.eu">www.javaspecialists.eu</a>
Java Champion - <a class="moz-txt-link-abbreviated" href="http://www.javachampions.org">www.javachampions.org</a>
JavaOne Rock Star Speaker
Tel: +30 69 75 595 262
Skype: kabutz
</pre>
    <div class="moz-cite-prefix">On 2021/11/25 07:12, Joe Bowbeer via
      Concurrency-interest wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAHzJPEq==X87fXc1S=Rc6BsvydT-s279mcGAudRNBKgA2oMQ7w@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div>
        <div dir="auto" style="border-color:rgb(0,0,0);color:rgb(0,0,0)">I
          like this original version without the vars. Maybe vars would
          be friendlier in an IDE with intellisense, but these javadoc
          examples should be obvious to the unassisted eye.</div>
        <div dir="auto" style="border-color:rgb(0,0,0);color:rgb(0,0,0)"><br>
        </div>
        <div dir="auto" style="border-color:rgb(0,0,0);color:rgb(0,0,0)">I
          would however remove all the extra space before the comments,
          but whatever style is used in the rest of the javadoc should
          prevail here.</div>
      </div>
      <div><br>
        <div class="gmail_quote">
          <div dir="ltr" class="gmail_attr">On Wed, Nov 24, 2021 at
            12:38 PM Doug Lea via Concurrency-interest &lt;<a
              href="mailto:[email protected]"
              moz-do-not-send="true">[email protected]</a>&gt;
            wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><br>
            On 11/24/21 2:15 PM, Dr Heinz M. Kabutz via
            Concurrency-interest wrote:<br>
            &gt; Every time I see the example in RecursiveTask I have to
            cringe:<br>
            <br>
            The initial rationale was that "nearly everyone" knows
            Fibonacci so it <br>
            doesn't need much explanation it. but you are right that
            even more <br>
            people know factorial, and the BigInteger version fits
            RecursiveTask <br>
            without needing caveats, so we should use it; thanks. Here's
            a javadoc'd <br>
            version of your example. Any objections to using it?<br>
            <br>
            <br>
              *<br>
              * &lt;pre&gt; {@code<br>
              * public class FactorialTask extends
            RecursiveTask&lt;BigInteger&gt; {<br>
              *   private final int from, to;<br>
              *   public FactorialTask(int n) { this(0, n); }<br>
              *   private FactorialTask(int from, int to) { this.from =
            from; <br>
            <a href="http://this.to" rel="noreferrer" target="_blank"
              moz-do-not-send="true">this.to</a> = to; }<br>
              *   protected BigInteger compute() {<br>
              *     if (from == to)                        // base case<br>
              *       return (from == 0) ? BigInteger.ONE :
            BigInteger.valueOf(from);<br>
              *     int mid = (from + to) &gt;&gt;&gt; 1;           //
            split in half<br>
              *     FactorialTask leftTask = (new FactorialTask(from,
            mid)).fork();<br>
              *     FactorialTask rightTask = new FactorialTask(mid + 1,
            to);<br>
              *     BigInteger right = rightTask.invoke(); // perform
            half the work <br>
            locally<br>
              *     BigInteger left = leftTask.join();<br>
              *     return left.multiply(right);<br>
              *   }<br>
              * }}&lt;/pre&gt;<br>
              *<br>
            <br>
            _______________________________________________<br>
            Concurrency-interest mailing list<br>
            <a href="mailto:[email protected]"
              target="_blank" moz-do-not-send="true">[email protected]</a><br>
            <a
              href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest"
              rel="noreferrer" target="_blank" moz-do-not-send="true">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a><br>
          </blockquote>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
Concurrency-interest mailing list
<a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>
<a class="moz-txt-link-freetext" href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a>
</pre>
    </blockquote>
  </body>
</html>

--===============8613832391509857264==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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

--===============8613832391509857264==--