Re: Better RecursiveTask Example
"Dr Heinz M. Kabutz via Concurrency-interest" <[email protected]> Wed, 24 Nov 2021 22:55:17 +0200
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <CACLL95qHM_YHkzpbwxS6tJnhj_U_RCBGM3betRYV3KSE1U6TNg@mail.gmail.com> |
--===============7839911998885568605== Content-Type: multipart/alternative; boundary="000000000000ed754b05d18f1306" --000000000000ed754b05d18f1306 Content-Type: text/plain; charset="UTF-8" Please go ahead and use it :-) On Wed, 24 Nov 2021 at 22:42, 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 > -- Dr Heinz M. Kabutz (PhD CompSci) Author of "The Java(tm) Specialists' Newsletter" Sun/Oracle Java Champion JavaOne Rockstar Speaker http://www.javaspecialists.eu Tel: +30 69 75 595 262 Skype: kabutz --000000000000ed754b05d18f1306 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"auto">Please go ahead and use it :-)</div><div><br><div class= =3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, 24 Nov 2021 = at 22:42, Doug Lea via Concurrency-interest <<a href=3D"mailto:concurren= [email protected]">[email protected]</a>> wrote= :<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bor= der-left:1px #ccc solid;padding-left:1ex"><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>-- <br><div dir=3D"ltr" class=3D"gmail_signature" = data-smartmail=3D"gmail_signature">Dr Heinz M. Kabutz (PhD CompSci)<br>Auth= or of "The Java(tm) Specialists' Newsletter"<br>Sun/Oracle Ja= va Champion<br>JavaOne Rockstar Speaker<br><a href=3D"http://www.javaspecia= lists.eu" target=3D"_blank">http://www.javaspecialists.eu</a><br>Tel: +30 6= 9 75 595 262<br>Skype: kabutz<br></div> --000000000000ed754b05d18f1306-- --===============7839911998885568605== 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 --===============7839911998885568605==--