I am using a PriorityQueue
across multiple threads. I am not sure it's a thread safe data-structure.
Is it safe to use scala.collection.mutable.PriorityQueue
across multiple threads?
Since PriorityQueue is not thread-safe, java provides PriorityBlockingQueue class that implements the BlockingQueue interface to use in a java multithreading environment.
A MessageService object is effectively immutable since its state can't change after its construction. So, it's thread-safe. Moreover, if MessageService were actually mutable, but multiple threads only have read-only access to it, it's thread-safe as well.
The HashMap in the Scala standard library is not thread-safe. This means that if multiple fibers are accessing the same key, and trying to modify the value, this can lead to inconsistent results.
Note: Despite being an immutable collection, the implementation uses mutable state internally during construction. These state changes are invisible in single-threaded code but can lead to race conditions in some multi-threaded scenarios.
TL;DR: It is not safe.
Let's have a look! scala.collection.mutable.PriorityQueue uses
private val resarr = new ResizableArrayAccess[A]
where ResizableArrayAccess is defined inside PriorityQueue as
private class ResizableArrayAccess[A] extends AbstractSeq[A] with ResizableArray[A] with Serializable {
From there, going to scala.collection.mutable.ResizableArray
, it is obvious that this is not thread safe, e.g. by looking at the update method:
protected var array: Array[AnyRef] = new Array[AnyRef](math.max(initialSize, 1))
// ... snip ... //
def update(idx: Int, elem: A) {
if (idx >= size0) throw new IndexOutOfBoundsException(idx.toString)
array(idx) = elem.asInstanceOf[AnyRef]
}
So we have unprotected access to a mutable var and usage of scala.collection.mutable.PriorityQueue
from multiple Threads is not encouraged.
If you really need to use it from multiple threads and concurrency is not important you can use some meaning of synchronization, e.g. scala.util.SyncVar
to protect against concurrency issues like race conditions. Otherwise using another data structure is better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With