Re: Better RecursiveTask Example
Joe Bowbeer via Concurrency-interest <[email protected]> Wed, 24 Nov 2021 21:12:04 -0800
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <CAHzJPEq==X87fXc1S=Rc6BsvydT-s279mcGAudRNBKgA2oMQ7w@mail.gmail.com> |
--===============7141219904013401218== Content-Type: multipart/alternative; boundary="00000000000092f9fc05d19604a0" --00000000000092f9fc05d19604a0 Content-Type: text/plain; charset="UTF-8" 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. 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. On Wed, Nov 24, 2021 at 12:38 PM Doug Lea via Concurrency-interest < [email protected]> wrote: > > On 11/24/21 2:15 PM, Dr Heinz M. Kabutz via Concurrency-interest wrote: > > Every time I see the example in RecursiveTask I have to cringe: > > The initial rationale was that "nearly everyone" knows Fibonacci so it > doesn't need much explanation it. but you are right that even more > people know factorial, and the BigInteger version fits RecursiveTask > without needing caveats, so we should use it; thanks. Here's a javadoc'd > version of your example. Any objections to using it? > > > * > * <pre> {@code > * public class FactorialTask extends RecursiveTask<BigInteger> { > * private final int from, 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) >>> 1; // split in half > * FactorialTask leftTask = (new FactorialTask(from, mid)).fork(); > * FactorialTask rightTask = new FactorialTask(mid + 1, to); > * BigInteger right = rightTask.invoke(); // perform half the work > locally > * BigInteger left = leftTask.join(); > * return left.multiply(right); > * } > * }}</pre> > * > > _______________________________________________ > Concurrency-interest mailing list > [email protected] > http://cs.oswego.edu/mailman/listinfo/concurrency-interest > --00000000000092f9fc05d19604a0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div><div dir=3D"auto" style=3D"border-color:rgb(0,0,0);color:rgb(0,0,0)">I= like this original version without the vars. Maybe vars would be friendlie= r in an IDE with intellisense, but these javadoc examples should be obvious= to the unassisted eye.</div><div dir=3D"auto" style=3D"border-color:rgb(0,= 0,0);color:rgb(0,0,0)"><br></div><div dir=3D"auto" style=3D"border-color:rg= b(0,0,0);color:rgb(0,0,0)">I would however remove all the extra space befor= e the comments, but whatever style is used in the rest of the javadoc shoul= d prevail here.</div></div><div><br><div class=3D"gmail_quote"><div dir=3D"= ltr" class=3D"gmail_attr">On Wed, Nov 24, 2021 at 12:38 PM Doug Lea via Con= currency-interest <<a href=3D"mailto:[email protected]"= >[email protected]</a>> wrote:<br></div><blockquote cla= ss=3D"gmail_quote" style=3D"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> > 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&= #39;d <br> version of your example. Any objections to using it?<br> <br> <br> =C2=A0=C2=A0*<br> =C2=A0=C2=A0* <pre> {@code<br> =C2=A0=C2=A0* public class FactorialTask extends RecursiveTask<BigIntege= r> {<br> =C2=A0=C2=A0*=C2=A0=C2=A0 private final int from, to;<br> =C2=A0=C2=A0*=C2=A0=C2=A0 public FactorialTask(int n) { this(0, n); }<br> =C2=A0=C2=A0*=C2=A0=C2=A0 private FactorialTask(int from, int to) { this.fr= om =3D from; <br> <a href=3D"http://this.to" rel=3D"noreferrer" target=3D"_blank">this.to</a>= =3D to; }<br> =C2=A0=C2=A0*=C2=A0=C2=A0 protected BigInteger compute() {<br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0 if (from =3D=3D to)=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // base case<br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return (from =3D=3D 0) ? = BigInteger.ONE : BigInteger.valueOf(from);<br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0 int mid =3D (from + to) >>> = 1;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // split in = half<br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0 FactorialTask leftTask =3D (new Facto= rialTask(from, mid)).fork();<br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0 FactorialTask rightTask =3D new Facto= rialTask(mid + 1, to);<br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0 BigInteger right =3D rightTask.invoke= (); // perform half the work <br> locally<br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0 BigInteger left =3D leftTask.join();<= br> =C2=A0=C2=A0*=C2=A0=C2=A0=C2=A0=C2=A0 return left.multiply(right);<br> =C2=A0=C2=A0*=C2=A0=C2=A0 }<br> =C2=A0=C2=A0* }}</pre><br> =C2=A0=C2=A0*<br> <br> _______________________________________________<br> Concurrency-interest mailing list<br> <a href=3D"mailto:[email protected]" target=3D"_blank">Con= [email protected]</a><br> <a href=3D"http://cs.oswego.edu/mailman/listinfo/concurrency-interest" rel= =3D"noreferrer" target=3D"_blank">http://cs.oswego.edu/mailman/listinfo/con= currency-interest</a><br> </blockquote></div></div> --00000000000092f9fc05d19604a0-- --===============7141219904013401218== 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 --===============7141219904013401218==--