new SerialNum example for ThreadLocal class doc

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

new SerialNum example for ThreadLocal class doc

Pete.Soper
Recently the "SerialNum" example class in the java.lang.ThreadLocal doc
was upgraded to use autoboxing and generics and to avoid unclear use of
"synchronized." We (some JSR166 expert group members and I) thought this
new version looked pretty good, but Tom Hawtin thought otherwise,
pointing out the value of using @Overload, "final" and
getAndIncrement(). That he found three ways to improve this was a bit of
a surprise and suggests the more eyes the better, so I'm asking for any
additional comments for the new version listed below. This is the raw
text and the escape sequences of course display as "<" and "@" as HTML.
Note also the two long lines format fine despite the autowrap your mail
client might show below.

-Pete

----
  * <pre>
  * import java.util.concurrent.atomic.AtomicInteger;
  *
  * public class SerialNum {
  *
  *     // The next serial number to be assigned. Starts at zero.
  *     private static final AtomicInteger nextSerialNum = new
AtomicInteger();
  *
  *     private static final ThreadLocal&lt;Integer> serialNum = new
ThreadLocal&lt;Integer>() {
  *         &#64;Override
  *         protected Integer initialValue() {
  *             return nextSerialNum.getAndIncrement();
  *         }
  *     };
  *
  *     public static int get() {
  *         return serialNum.get();
  *     }
  * }</pre>



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Need one more reviewer, please Re: new SerialNum example for ThreadLocal class doc

Pete.Soper
I need one more name (email address fine) of somebody who has carefully
examined this example and judged it to be the right thing for Mustang.
Send this directly to me, not to the list, please. If there's anything
to report back I'll be sure to do that.

Thanks,
Pete

Pete Soper wrote:

> Recently the "SerialNum" example class in the java.lang.ThreadLocal doc
> was upgraded to use autoboxing and generics and to avoid unclear use of
> "synchronized." We (some JSR166 expert group members and I) thought this
> new version looked pretty good, but Tom Hawtin thought otherwise,
> pointing out the value of using @Overload, "final" and
> getAndIncrement(). That he found three ways to improve this was a bit of
> a surprise and suggests the more eyes the better, so I'm asking for any
> additional comments for the new version listed below. This is the raw
> text and the escape sequences of course display as "<" and "@" as HTML.
> Note also the two long lines format fine despite the autowrap your mail
> client might show below.
>
> -Pete
>
> ----
>   * <pre>
>   * import java.util.concurrent.atomic.AtomicInteger;
>   *
>   * public class SerialNum {
>   *
>   *     // The next serial number to be assigned. Starts at zero.
>   *     private static final AtomicInteger nextSerialNum = new
> AtomicInteger();
>   *
>   *     private static final ThreadLocal&lt;Integer> serialNum = new
> ThreadLocal&lt;Integer>() {
>   *         &#64;Override
>   *         protected Integer initialValue() {
>   *             return nextSerialNum.getAndIncrement();
>   *         }
>   *     };
>   *
>   *     public static int get() {
>   *         return serialNum.get();
>   *     }
>   * }</pre>
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Need one more reviewer, please Re: new SerialNum example for ThreadLocal class doc

Pete.Soper
[hidden email] suggested "Why not remove 'Starts at zero' and give an
explicit 0 argument to the AtomicInteger constructor?" and I said I
think this is a good idea. It's incorporated below. Any more comments
(direct to me, please)?

-Pete

----
   * <pre>
   * import java.util.concurrent.atomic.AtomicInteger;
   *
   * public class SerialNum {
   *
   *     private static final AtomicInteger nextSerialNum = new
AtomicInteger(0);
   *
   *     private static final ThreadLocal&lt;Integer> serialNum = new
ThreadLocal&lt;Integer>() {
   *         &#64;Override
   *         protected Integer initialValue() {
   *             return nextSerialNum.getAndIncrement();
   *         }
   *     };
   *
   *     public static int get() {
   *         return serialNum.get();
   *     }
   * }</pre>



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Need one more reviewer, please Re: new SerialNum example for ThreadLocal class doc

Pete.Soper
Thanks for all the high quality input. I will send a 'final' version for
review but I have to finish the unit test and I picked a bad time to
come up to speed on NetBeans (but it is *absolutely fabulous* and I
haven't had such a cool tool as this in a long time).

But here's the "current" version sans "*" and HTML escapes.
Josh: I'm using latest jstyle and will get that sorted if this version
isn't right yet. Thanks for the gentle feedback.
Brian: I will get back to that other example with you soonest.

(again, comments directly to me, please)

I should also note that because the example starting this review was a
"suggested fix" for a bug I'm not constrained by Sun licensing
restrictions. It would not be wise to cut and paste source out of the
JDk and do this kind of thing (at least not yet!).

-Pete
--------------
import java.util.concurrent.atomic.AtomicInteger;

public class UniqueThreadIdGenerator {

     private static final AtomicInteger i = new AtomicInteger(0);

     private static final ThreadLocal<Integer> uniqueNum =
         new ThreadLocal<Integer>() {
             @Override protected Integer initialValue() {
                 return i.getAndIncrement();
             }
     };

     public static int getCurrentThreadId() {
         return i.get();
     }
} // UniqueThreadIdGenerator
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest