Which is “better”. AtomicIntegerArray (1/0 as true/false) versus AtomicBoolean[]?

后端 未结 3 2421
孤街浪徒
孤街浪徒 2021-02-20 11:08

I am very curious about that. If you use AtomicIntegerArray with values 0 and 1 you can accomplish the same thing of an AtomicBoolean array. Example:

final Atomi         


        
3条回答
  •  自闭症患者
    2021-02-20 11:31

    Actually watching the implementation of AtomicIntegerArray

    http://fuseyism.com/classpath/doc/java/util/concurrent/atomic/AtomicIntegerArray-source.html

    it seem that it is managed with more attention then I thought.

    It doesn't use Objects to store the values, making it more efficient in memory. In fact it uses a simple int[] and then access them in a safe way.

    So I think that if you need to use many AtomicInteger it is better to use the AtomicIntegerArray.

    AtomicIntegerArray: uses the Unsafe class to make atomic access to a single int[] in the AtomicIntegerArray

    AtomicBoolean[]: every single object of the array has it's object(itself) for making atomic access

    So I would expect a better performance in a heavy concurrent threaded environment with an AtomicBoolean[], with more memory consumption than the AtomicIntegerArray.

提交回复
热议问题