final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; Object[] newElements = Arrays.copyOf(elements, len + 1); newElements[len] = e; setArray(newElements); return true; } finally { lock.unlock(); }
In which the volatile keyword:
ensure consistency of system memory
For example: a plurality of CPU, the CPU 1 performs the update + 1 operation, the number of memory will pull cpu2 be modified when making changes to ensure successful memory update + 1 when modified