1 2 3 Previous Next 36 Replies Latest reply: Aug 16, 2007 1:35 PM by 796440 RSS

    Atomic operation on array elements

    807605
      For array elements of primitive types (32 bits and less): byte, short, int. Are array operations atomic?

      For example, define byte b[] = new byte[1];
      thread A executes b[0]++; thread B reads b[0]. or both threads writing..

      Question:
      1 Is it possible to get an incostintent value reading b[0].
      2. Do you think JVM or OS know that two threads work on the same element and synchronizes them to ensure value consistency?

      As a consequence of synchronizatrion, is there speed difference if I have two threads writing to b[0] and b[1] OR two threads writing to same element b[0]. I checked, there seems to be none.
        • 1. Re: Atomic operation on array elements
          807605
          threads writing
          Increment itself isn't guaranteed to be atomic, so it two threads are
          executing n++ on the same variable, there's no guarantee that
          the variable's value will increase by 2. Note this have nothing
          to do with array.
          • 2. Re: Atomic operation on array elements
            807605
            so how's this relevant to my post - i'm aware of simple threading?
            • 3. Re: Atomic operation on array elements
              807605
              Woops, I was trying to reply to the Ben Kingsley, Mary-Kate Olsen thread.
              Sorry, that have nothing to do with multi-threading.
              • 4. Re: Atomic operation on array elements
                baftos
                Primitive types, in an array or not, do not have "atomic operations",
                whatever this means. Java Language Specification mandates
                that reads and writes be atomic, except for long and double.
                If two threads execute on the same int variable n:
                n = 0x0000FFFF;
                and
                n = 0xFFFF0000;
                The result will be either 0x0000FFFF or 0xFFFF0000 and never
                0x00000000 or 0xFFFFFFFF.
                Same for an int array[].
                array[0] = 0x0000FFFF;
                and
                array[0] = 0xFFFF0000;
                has the same beahvior.
                • 5. Re: Atomic operation on array elements
                  791266
                  The result will be either 0x0000FFFF or 0xFFFF0000
                  and never
                  0x00000000 or 0xFFFFFFFF.
                  The result (when using a primitive) can actually be both 0x0000FFFF and 0xFFFF0000 unless the variable is volatile. (The two threads aren't forced to write the value to main memory)
                  • 6. Re: Atomic operation on array elements
                    807605
                    To answer the original questions:

                    1. Yes.
                    2. Not in this lifetime.

                    Make the array volatile. This way reading threads will get the latest value. Use sync on the writes. This way the writing threads will be managed.

                    Or, you can use an array of AtomicInteger
                    • 7. Re: Atomic operation on array elements
                      807605
                      Make the array volatile.
                      I know I've been chastised and sent to the Mary-Kate thread, but I doubt that will work, since only the array reference itself will be volatile.
                      • 8. Re: Atomic operation on array elements
                        796440
                        Make the array volatile.
                        That will not help for two reasons.

                        1) volatile will apply to the array reference variable, not its elements.

                        2) Even if it did apply to the elements ++ involves both a read and a write, which will still be separate operations.

                        The only way to make ++ atomic, whether on an array or scalar variable, is with synchronization.
                        • 9. Re: Atomic operation on array elements
                          807605
                          OK, no volatile.

                          But AtomicInteger works just fine.
                          • 10. Re: Atomic operation on array elements
                            baftos
                            The result will be either 0x0000FFFF or 0xFFFF0000
                            and never
                            0x00000000 or 0xFFFFFFFF.
                            The result (when using a primitive) can actually be
                            both 0x0000FFFF and 0xFFFF0000 unless the variable is
                            volatile. (The two threads aren't forced to write the
                            value to main memory)
                            Oh, thanks for the clarification. After reading your post, I made a small
                            research. Correct me if I'm wrong, but the way I understand it now is
                            that, without volatile, the value can be either:
                            0x0000FFFF or 0xFFFF0000 or whatever value was there before the
                            thread(s) executed their code
                            .
                            • 11. Re: Atomic operation on array elements
                              796440
                              Oh, thanks for the clarification. After reading your
                              post, I made a small
                              research. Correct me if I'm wrong, but the way I
                              understand it now is
                              that, without volatile, the value can be either:
                              0x0000FFFF or 0xFFFF0000 or whatever value was
                              there before the
                              thread(s) executed their code
                              .
                              Without volatile, one thread could see 0x0000FFFF and the other could see 0xFFFF0000, as each thread can have its own local copy.

                              In addition, if there's a third thread, then it might see either of those values, or it might see whatever was last written to the main copy--what you're referring to as "before" I guess.

                              With volatile, all threads must always read and write from the main copy.
                              • 12. Re: Atomic operation on array elements
                                807605
                                Guys,
                                I know about atomic variables and the use volatile. Volatile IS NOT related to this topic. Atomic operation is. I have an array. So volatile doesn't apply here anyway (jverd pointed it out).

                                A lot of people don't read the question it seems

                                Suppose I do not want to synchronize access to array elements. It's going degrade performance but would like to understand whether JVM enforces the same atomic rule on array elements as does with variables. (i'm implying array of bytes or ints)

                                Can anynody comment on performance? Is it faster to modify different elements of array versus the same index? shouldn't the same be slower?
                                • 13. Re: Atomic operation on array elements
                                  807605
                                  We speak of atomic operations on atomic types.

                                  atomic operation on variable means it could be changed at once or not. your program (thread or process) is only going to see either an old value or new. Never a transient value as the bits are reset.

                                  shall I provide an example with type long to illustrate my point -it's very helpful?

                                  Moreover, a preemptive thread model which stops your thread anywhere it likes is ALWAYS going to let the atomic operation complete. For example your thread was changing the value of int. and operating system decided to pause your thread. The change to variable will either go through or not be started depending on timing.

                                  is this clear?
                                  • 14. Re: Atomic operation on array elements
                                    796440
                                    Suppose I do not want to synchronize access to array
                                    elements.
                                    Then you can't guarantee correct behavior of things like ++.

                                    It's going degrade performance
                                    Have you measured this, so that you know it will be a problem, or are you just assuming?

                                    but would
                                    like to understand whether JVM enforces the same
                                    atomic rule on array elements as does with variables.
                                    ++ is NOT atomic on anything--variables or array elements. All threading issues are the same for array elements as for variables.


                                    Can anynody comment on performance? Is it faster to
                                    modify different elements of array versus the same
                                    index? shouldn't the same be slower?
                                    Try it yourself and see if you can even see a meaningful difference.
                                    1 2 3 Previous Next