[JBoss-dev] is x++ an atomic operation??
Quick question for you Java Language Gurus out there, I heard one that the post increment operator was an atomic operation. For example, if you have a multi-threaded application with: id=lastRequestId++; You would not need to put this in a synchronized block be cause the ++ would be atomic and thus you would not get 2 duplicate ids. I was wondering if this is true or not. Can anybody confirm this for me?? Regards, Hiram --- 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
Re: [JBoss-dev] is x++ an atomic operation??
If lastRequestId is a long, then it clearly is not. I don't think that it is in general either...not sure if that changes with hotspot. And while it may be under certain VMs, I don't think that you can generalize that. Brian Original Message Follows From: Hiram Chirino [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: [JBoss-dev] is x++ an atomic operation?? Date: Thu, 18 Jul 2002 08:54:43 -0400 Quick question for you Java Language Gurus out there, I heard one that the post increment operator was an atomic operation. For example, if you have a multi-threaded application with: id=lastRequestId++; You would not need to put this in a synchronized block be cause the ++ would be atomic and thus you would not get 2 duplicate ids. I was wondering if this is true or not. Can anybody confirm this for me?? Regards, Hiram _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx --- 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
RE: [JBoss-dev] is x++ an atomic operation??
I'm fairly sure that the increment/decrement operators are not atomic. Hm...here's a post on Jguru showing why: http://www.jguru.com/forums/view.jsp?EID=384082 Additionally, I think the statement that you gave in your example would be non-atomic anyway- even if an increment was atomic, you're first performing the increment, then storing it in a second variable. That's a two-step process, regardless of atomicity in the increment. -Original Message- From: Hiram Chirino [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 18, 2002 8:55 AM To: [EMAIL PROTECTED] Subject: [JBoss-dev] is x++ an atomic operation?? Quick question for you Java Language Gurus out there, I heard one that the post increment operator was an atomic operation. For example, if you have a multi-threaded application with: id=lastRequestId++; You would not need to put this in a synchronized block be cause the ++ would be atomic and thus you would not get 2 duplicate ids. I was wondering if this is true or not. Can anybody confirm this for me?? Regards, Hiram --- 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
Re: [JBoss-dev] is x++ an atomic operation??
The jguru article is not accurate. Given the code: public class Test { public int testInc(int x) { x++; return x; } public int testAdd(int x) { x = x + 1; return x; } } This produces the following byte-code: Method Test() 0 aload_0 1 invokespecial #1 Method java.lang.Object() 4 return Method int testInc(int) 0 iinc 1 1 3 iload_1 4 ireturn Method int testAdd(int) 0 iload_1 1 iconst_1 2 iadd 3 istore_1 4 iload_1 5 ireturn Note the single operation for the incrementor (iinc 1 1). I do not know if this operation is atomic or not, but this invalidates the logic of the jguru post. -Larry - Original Message - From: Rhett Aultman [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, July 18, 2002 6:24 AM Subject: RE: [JBoss-dev] is x++ an atomic operation?? I'm fairly sure that the increment/decrement operators are not atomic. Hm...here's a post on Jguru showing why: http://www.jguru.com/forums/view.jsp?EID=384082 Additionally, I think the statement that you gave in your example would be non-atomic anyway- even if an increment was atomic, you're first performing the increment, then storing it in a second variable. That's a two-step process, regardless of atomicity in the increment. -Original Message- From: Hiram Chirino [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 18, 2002 8:55 AM To: [EMAIL PROTECTED] Subject: [JBoss-dev] is x++ an atomic operation?? Quick question for you Java Language Gurus out there, I heard one that the post increment operator was an atomic operation. For example, if you have a multi-threaded application with: id=lastRequestId++; You would not need to put this in a synchronized block be cause the ++ would be atomic and thus you would not get 2 duplicate ids. I was wondering if this is true or not. Can anybody confirm this for me?? Regards, Hiram --- 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 --- 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
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. -Original Message- From: Larry Sandereson [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 18, 2002 9:32 AM To: [EMAIL PROTECTED] Subject: Re: [JBoss-dev] is x++ an atomic operation?? The jguru article is not accurate. Given the code: public class Test { public int testInc(int x) { x++; return x; } public int testAdd(int x) { x = x + 1; return x; } } This produces the following byte-code: Method Test() 0 aload_0 1 invokespecial #1 Method java.lang.Object() 4 return Method int testInc(int) 0 iinc 1 1 3 iload_1 4 ireturn Method int testAdd(int) 0 iload_1 1 iconst_1 2 iadd 3 istore_1 4 iload_1 5 ireturn Note the single operation for the incrementor (iinc 1 1). I do not know if this operation is atomic or not, but this invalidates the logic of the jguru post. -Larry - Original Message - From: Rhett Aultman [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, July 18, 2002 6:24 AM Subject: RE: [JBoss-dev] is x++ an atomic operation?? I'm fairly sure that the increment/decrement operators are not atomic. Hm...here's a post on Jguru showing why: http://www.jguru.com/forums/view.jsp?EID=384082 Additionally, I think the statement that you gave in your example would be non-atomic anyway- even if an increment was atomic, you're first performing the increment, then storing it in a second variable. That's a two-step process, regardless of atomicity in the increment. -Original Message- From: Hiram Chirino [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 18, 2002 8:55 AM To: [EMAIL PROTECTED] Subject: [JBoss-dev] is x++ an atomic operation?? Quick question for you Java Language Gurus out there, I heard one that the post increment operator was an atomic operation. For example, if you have a multi-threaded application with: id=lastRequestId++; You would not need to put this in a synchronized block be cause the ++ would be atomic and thus you would not get 2 duplicate ids. I was wondering if this is true or not. Can anybody confirm this for me?? Regards, Hiram --- 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 --- 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
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
Re: [JBoss-dev] is x++ an atomic operation??
Hiram, I this code is at least 2 operations. Most of the atomic operations (except int assignment) are completely useless because you can't both invoke the operator and get the result in a single operation. For example: a: increment i (i=1) b: increment i (i=2) a: assign i to out (out = 2) b: assign i to out (out = 2) This discussion and the tx discussion are both running in the dark; you don't know where you are and you don't know where you are going... work for work sake. If you synchronized the code, what is the actual performance impact? I can tell you that with Hot Spot the code is very little. The real cost happens when you have lots of contention. Do you have lots of contention for this code? I always say synchronize it and if it is a real performance problem we will fix it during tuning. -dain Hiram Chirino wrote: Quick question for you Java Language Gurus out there, I heard one that the post increment operator was an atomic operation. For example, if you have a multi-threaded application with: id=lastRequestId++; You would not need to put this in a synchronized block be cause the ++ would be atomic and thus you would not get 2 duplicate ids. I was wondering if this is true or not. Can anybody confirm this for me?? Regards, Hiram --- 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 -- Dain Sundstrom Chief Architect JBossCMP JBoss Group, LLC --- 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
Re: [JBoss-dev] is x++ an atomic operation??
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
Re: [JBoss-dev] is x++ an atomic operation??
Being a lazy programmer, I've always found that if I'm spending my few remaining neurons trying to figure out what operation is truly atomic and what isn't, I'll be better off designing the system in such a way that it doesn't matter. I've spent enough time grovelling through disassembled compiler output trying to figure out how Visual C++ had screwed me this time to never want to go through the experience again. Put another way - the fact that you need to know if an operation is atomic means that you have a critical section. Since you have a critical section, synchronize it and keep it short. -danch Dain Sundstrom wrote: Hiram, I this code is at least 2 operations. Most of the atomic operations (except int assignment) are completely useless because you can't both invoke the operator and get the result in a single operation. For example: a: increment i (i=1) b: increment i (i=2) a: assign i to out (out = 2) b: assign i to out (out = 2) This discussion and the tx discussion are both running in the dark; you don't know where you are and you don't know where you are going... work for work sake. If you synchronized the code, what is the actual performance impact? I can tell you that with Hot Spot the code is very little. The real cost happens when you have lots of contention. Do you have lots of contention for this code? I always say synchronize it and if it is a real performance problem we will fix it during tuning. -dain Hiram Chirino wrote: Quick question for you Java Language Gurus out there, I heard one that the post increment operator was an atomic operation. For example, if you have a multi-threaded application with: id=lastRequestId++; You would not need to put this in a synchronized block be cause the ++ would be atomic and thus you would not get 2 duplicate ids. I was wondering if this is true or not. Can anybody confirm this for me?? Regards, Hiram --- 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