8343250: ArrayBlockingQueue serialization not thread safe

Reviewed-by: rriggs, alanb
This commit is contained in:
Dr Heinz M. Kabutz 2024-11-12 11:34:11 +00:00 committed by Viktor Klang
parent 2c1e4c3816
commit 5729227651

View File

@ -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<E> extends AbstractQueue<E>
&& (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).
*