Ah - you are correct.  Here is the byte-code for the incrementor with a
member variable:

Method Test()
   0 aload_0
   1 invokespecial #1 <Method java.lang.Object()>
   4 return

Method int testInc()
   0 aload_0
   1 dup
   2 getfield #2 <Field int x>
   5 iconst_1
   6 iadd
   7 putfield #2 <Field int x>
  10 aload_0
  11 getfield #2 <Field int x>
  14 ireturn

Method int testAdd()
   0 aload_0
   1 aload_0
   2 getfield #2 <Field int x>
   5 iconst_1
   6 iadd
   7 putfield #2 <Field int x>
  10 aload_0
  11 getfield #2 <Field int x>
  14 ireturn

These two methods are now identical, and obviously not atomic.  Thanks.

-Larry

----- Original Message -----
From: "Kevin Conner" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, July 18, 2002 7:19 AM
Subject: RE: [JBoss-dev] is x++ an atomic operation??


> > You know...something seemed odd about it, but I was going to
> > go with it anyway...I'm still pretty sure that an increment
> > operation is not atomic, but I can't be sure.
> >
> > Regardless, the example statement given in the original post
> > was something like:
> >
> > x = y++;
> >
> > And I'm almost 100% certain that's not atomic, as the
> > increment of y and assignment to x are two separate operations.
>
> The increment of y is not guaranteed to be atomic as it involves
> a read, an increment and then a write.  These operations may be
> interleaved with operations on another thread.  This is before
> you have the assigment to x.
>
> The iinc operator is only for local method variables and therefore
> not visible in other threads.  If you change the code to the following
> then you should see different bytecode.
>
> public class Test {
>
>     private int x ;           // instance variable
>
>     public int testInc() {
>         x++;
>         return x;
>     }
>     public int testAdd() {
>         x = x + 1;
>         return x;
>     }
> }
>
> Kev
>
> Kevin Conner
> Orchard Information Systems Limited
> Newcastle Technopole, Kings Manor
> Newcastle Upon Tyne, NE1 6PA. United Kingdom
> Registered in England, Number 1900078
> Tel: +44 (0) 191-2032536  Fax: +44 (0) 191 2302515
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Jboss-development mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/jboss-development
>



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to