I am looking for patterns and api for managing with a Java object that is not thread safe, in the case that a parallel computation needs to use this object. This is applicable to database connections as well as other kinds of objects in my case (and in general).
The specific parallel scenario I am looking at is simply a Scala parallel collection that is being foreach
ed, whereas within the foreach
clause, the object needs to be used. Since the Java object at hand is mutable and not safe for concurrency, that is not supposed to work.
There is a nice suggestion here for that, in pure Java, and other ones also Java. I wonder if higher elegance can be accomplished in Scala.