is scala.collection.mutable.Queue thread safe?

3,699 views
Skip to first unread message

Andy C

unread,
Mar 7, 2014, 9:45:47 AM3/7/14
to scala-user
Hi,

This is a follow-up to my previous thread. Question is as in subject. The documentation  (http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html#mutable_queues) is not clear.

There is also a synchronized version of the queue, but that seems to be very old code and it is just a version of Queue with a synchronized guard on top of it, which seems to be unnecessary for typical producer consumer problem.

Thx,
Andy

√iktor Ҡlang

unread,
Mar 7, 2014, 10:04:42 AM3/7/14
to Andy C, scala-user
For the JVM—assume that something is thread unsafe unless explicitly stated otherwise.


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

———————
Viktor Klang
Chief Architect - Typesafe

Twitter: @viktorklang

Andy C

unread,
Mar 7, 2014, 12:57:58 PM3/7/14
to √iktor Ҡlang, scala-user


On Fri, Mar 7, 2014 at 8:04 AM, √iktor Ҡlang <viktor...@gmail.com> wrote:
For the JVM—assume that something is thread unsafe unless explicitly stated otherwise.


So if scala.collection.mutable.Queue is thread safe, what is the purpose of scala.collection.mutable.SynchronizedQueue ?

Andy

Nils Kilden-Pedersen

unread,
Mar 7, 2014, 1:05:05 PM3/7/14
to Andy C, √iktor Ҡlang, scala-user
On Fri, Mar 7, 2014 at 11:57 AM, Andy C <andy.c...@gmail.com> wrote:



On Fri, Mar 7, 2014 at 8:04 AM, √iktor Ҡlang <viktor...@gmail.com> wrote:
For the JVM—assume that something is thread unsafe unless explicitly stated otherwise.


So if scala.collection.mutable.Queue is thread safe

Who said it is?
 
, what is the purpose of scala.collection.mutable.SynchronizedQueue ?

Andy

Andy C

unread,
Mar 7, 2014, 3:04:23 PM3/7/14
to scala-user
So if scala.collection.mutable.Queue is thread safe

Who said it is?

After reading this http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html and other Scala material I somehow made that assumption.

I never been Java/JVM person and as a mere Scala language consumer here would expect that certain things are spelled out, especially that Scala comes with the promise of being safe in the world of concurrent programming.

Andy

Tim Pigden

unread,
Mar 7, 2014, 3:32:06 PM3/7/14
to Andy C, scala-user
Andy - If you want easier concurrency, you should be looking at immutable stuff and a functional approach and/or akka. You can use scala in the same way as any other imperative language - and use mutable collections - but then your concurrency issues will be exactly the same as in those in any other language. The reason scala is good for concurrency is that it readily supports you moving to the functional paradigm while maintaining familiar OOP idioms.
These days my code has almost no vars and makes almost no use of mutable structures. OK there are certain circumstances in which you might need to use a mutable data structure but you shouldn't bother until you have done proper instrumentation. Immutable data structures are surprisingly efficient. 



--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Tim Pigden
Optrak Distribution Software Limited
+44 (0)1992 517100
http://www.linkedin.com/in/timpigden
http://optrak.com
Optrak Distribution Software Ltd is a limited company registered in England and Wales.
Company Registration No. 2327613 Registered Offices: Suite 6,The Maltings, Hoe Lane, Ware, SG12 9LR England 
This email and any attachments to it may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of Optrak Distribution Software Ltd. If you are not the intended recipient of this email, you must neither take any action based upon its contents, nor copy or show it to anyone. Please contact the sender if you believe you have received this email in error.

Alexandru Nedelcu

unread,
Mar 11, 2014, 1:57:43 AM3/11/14
to Andy C, scala-user

Andy, the problem with mutable data-structures that claim to be thread-safe is that thread-safe operations are not composable. So if you do a data.get() before a data.set(), the result will be unsafe, making the whole thread-safety claim pretty useless, unless you have compare-and-swap semantics. This is why mutable data-structures on the JVM are usually not thread-safe, since locks have overhead and you'll need higher level locks anyway.

As Tim said, use an immutable queue and if mutability is required just stick that in an AtomicReference - best thing since slice bread.

--

Andy C

unread,
Apr 6, 2014, 3:58:15 PM4/6/14
to Alexandru Nedelcu, scala-user
On Fri, Mar 7, 2014 at 1:32 PM, Tim Pigden <tim.p...@optrak.com> wrote:
Andy - If you want easier concurrency, you should be looking at immutable stuff and a functional approach and/or akka.


With all due respect, Akka actors do not compose like functions at all.

 
These days my code has almost no vars and makes almost no use of mutable structures.

I have like few 1000s lines of code with only a few var  to manage state.

Thx,
Andy

Martijn Hoekstra

unread,
Apr 7, 2014, 11:32:39 AM4/7/14
to Andy C, Alexandru Nedelcu, scala-user
My dad can beat up both up your dads without a single var! 

Andy C

unread,
Apr 7, 2014, 1:51:47 PM4/7/14
to Martijn Hoekstra, Alexandru Nedelcu, scala-user

>My dad can beat up both up your dads without a single var!

:-)
Reply all
Reply to author
Forward
0 new messages