score:1
the problem of parallel access to the same data structure is twofold. first, we need to serialize parallel updates. @tim gave a comprehensive answer to this. second, in the case there are many readers, we may want to allow them to read in parallel with writing. and in this case immutability plays its role. without it, writers have to wait the readers to finish.
score:0
you may use cats ref type class https://typelevel.org/cats-effect/concurrency/ref.html but it is that atomicreference realization, or write some java.util.concurent.concurenthashmap wrapper
score:1
there isn't really a "functional" way to have a data structure that can be updated by multiple threads. in fact one reason that functional programming is so good in a multi-threaded environment is because there aren't any shared data structures like this.
however in the real world this problem comes up all the time, so you need some way to serialise access to the shared data structure. the most crude way is simply to put a big lock around the whole code and only allow one thread to run at once (e.g. with a mutex). with clever design this can be made reasonably efficient, but it can be difficult to get right and complicated to maintain.
a more sophisticated approach is to have a thread-safe queue of requests to your data structure and a single worker thread that processes these requests one-by-one. one popular framework that supports this model is akka actors. you wrap your data structure in an actor
which then receives requests to read or modify the data structure. the akka framework will ensure that only one message is processed at once.
in your case your the actor would manage the heap and receive updates from the stream which would go into the heap. other threads can then make requests that will be processes in a sequential, thread-safe way. it is best if these request perform specific queries on the heap, rather than just returning the whole heap every time.
Source: stackoverflow.com
Related Query
- How to synchronize insert/removal of elements to/from a data structure, the Functional Way?
- How to create all possible combinations from the elements of a list?
- AWS Lambda - How to get the topic name of data coming from AWS IOT
- How can I approximate the size of a data structure in scala?
- How we can sort and group data from the Spark RDDs?
- how to extract the column name and data type from nested struct type in spark
- How to create Data frame from csv in Spark(using scala) when the first line is the schema?
- How can I get the binary data from a Class[_] object?
- Lift: How to pass data from JavaScript back to the server
- How to access the data from streaming query in "memory" table for subsequent batch queries?
- How can I acqurie the JSON data from Kafka using SparkStreaming
- How to read input from a file and convert data lines of the file to List[Map[Int,String]] using scala?
- Scala 3: How do you extract the names of elements from a Mirror.Sum as a tuple?
- How to adapt the following data transformation from pyspark to scala
- How to generate the materialized value from the elements in Source or Flow?
- How to create the POJO to handle JSON data having array of elements as well as array of array of elements
- How to increase the default precision and scale while loading data from oracle using spark-sql
- How do I get the data from "#PCDATA" in Scala?
- How to find the elements present in a list from another list
- How to find the phrase count from data frame in spark scala?
- How to create a RDD from RC file using data which is partitioned in the hive table
- Get the elements from different arraytype columns and build a column with heterogeneous data in Spark
- How to show data from the RDF archive in scala flink
- How much trust should I put in the validity of retrieved data from database?
- How to create an tuple from all the Elements present in an Array
- How to read the data of the last 3 days from a folder with parquet files?
- How to get the sum of two elements from the Spark RDD Iiterable
- How to filter the same elements from two sources
- How to do sum on the elements from list of Objects in Scala in play html template?
- Order of the columns in Apache Zeppelin when selecting the data from the temprorary table is wrong, how to put specific column first?
More Query from same tag
- filtering only one side of a list/iterable in scala
- Why do I get `java.lang.NoClassDefFoundError: scala/Function1` when I run my code in ScalaIDE?
- How to put methods in sets?
- How to manage multiple stdout's from multiple external command runned in separate threads?
- trimming all string type columns dynamically of dataframe scala spark
- Calculate all permutations for List(s) inside List
- RasterFrames extracting location information problem
- Deal with futures in a loop
- scala : Match type argument for an object
- why am I not able to access object's property
- How do I get the TypeTag for an Array's element type?
- Extract a column value and assign it to another column as an array in Spark dataframe
- Scala Slick incompatible with Kerberos?
- Scala collection.breakOut doesn't work with case classes?
- Inner Join Spark (Scala)
- How to run scalatest specs with two different dependency injection containers?
- How does Scala specs2 sequential keyword work
- Scala 3 macros trait arguments
- how to make the Query to list in slick2?
- Scala scope saying that object.propery doesn't exist but left panel in eclipse shows it
- React with references in immutable data
- missing dependency detected during test using scalamock
- Type mismatch error of Unit instead of Int (Scala)
- SortByValue for a RDD of tuples
- Wiki xml parser - org.apache.spark.SparkException: Task not serializable
- scala filter by type
- Spark-Scala Question on Using Case Class Demiliter
- How to view akka dead letters
- Scala asInstanceOf generic type does not fail in Try
- Pattern matching doesn't work