diff --git a/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java b/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java index d52d18a06d3..b16f3459601 100644 --- a/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java @@ -35,6 +35,7 @@ package java.util.concurrent; +import java.io.IOException; import java.lang.ref.WeakReference; import java.util.AbstractQueue; import java.util.Arrays; @@ -1617,6 +1618,23 @@ public class ArrayBlockingQueue extends AbstractQueue && (count == 0 || items[dec(putIndex, capacity)] != null); } + /** + * Writes the queue to the stream whilst holding the locks to prevent + * broken invariants. + * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs + */ + private void writeObject(java.io.ObjectOutputStream s) throws IOException { + final ReentrantLock lock = this.lock; + lock.lock(); + try { + s.defaultWriteObject(); + } finally { + lock.unlock(); + } + } + /** * Reconstitutes this queue from a stream (that is, deserializes it). *