Volatile Creature

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

Volatile Creature

Hanson Char
Is Creature thread-safe ?  If so, is it more performant than if we
used AtomicLong as suggested in Puzzle 55 of Java Puzzlers ?

Hanson

class Creature {
    private static volatile long numCreated = 0;

    public Creature() {
        numCreated++;
    }

    public static long numCreated() {
        return numCreated;
    }
}

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

Re: Volatile Creature

Jeremy Manson

Hi Hanson,

This isn't thread safe (depending on your definition of thread safe).
The constructor is actually three separate operations:

{
   r1 = numCreated;
   r1 = r1 + 1;
   numCreated = r1;
}


If two Creatures are created simultaneously, then you may have a lost
update.

                                        Jeremy


Hanson Char wrote:
 > Is Creature thread-safe ?  If so, is it more performant than if we
 > used AtomicLong as suggested in Puzzle 55 of Java Puzzlers ?
 >
 > Hanson
 >
 > class Creature {
 >     private static volatile long numCreated = 0;
 >
 >     public Creature() {
 >         numCreated++;
 >     }
 >
 >     public static long numCreated() {
 >         return numCreated;
 >     }
 > }
 >
 > _______________________________________________
 > 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: Volatile Creature

tpeierls
In reply to this post by Hanson Char
Hanson Char wrote:

> Is Creature thread-safe ?  If so, is it more performant than if we
> used AtomicLong as suggested in Puzzle 55 of Java Puzzlers ?
>
> class Creature {
>     private static volatile long numCreated = 0;
>
>     public Creature() {
>         numCreated++;
>     }
>
>     public static long numCreated() {
>         return numCreated;
>     }
> }

No, because the increment operation (++) is not atomic.

--tim

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

Re: Volatile Creature

Hanson Char
In reply to this post by Jeremy Manson
Yes, of course, thanks.

The good old 3-in-1 increment operation.  I promise not t ask this
very same old question again.

:)
Hanson

On 12/14/05, Jeremy Manson <[hidden email]> wrote:

>
> Hi Hanson,
>
> This isn't thread safe (depending on your definition of thread safe).
> The constructor is actually three separate operations:
>
> {
>    r1 = numCreated;
>    r1 = r1 + 1;
>    numCreated = r1;
> }
>
>
> If two Creatures are created simultaneously, then you may have a lost
> update.
>
>                                         Jeremy
>
>
> Hanson Char wrote:
>  > Is Creature thread-safe ?  If so, is it more performant than if we
>  > used AtomicLong as suggested in Puzzle 55 of Java Puzzlers ?
>  >
>  > Hanson
>  >
>  > class Creature {
>  >     private static volatile long numCreated = 0;
>  >
>  >     public Creature() {
>  >         numCreated++;
>  >     }
>  >
>  >     public static long numCreated() {
>  >         return numCreated;
>  >     }
>  > }
>  >
>  > _______________________________________________
>  > 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