0

To better demonstrate the difficulties of reading/updating objects in a multithreaded environment, I'm looking for examples for groups of objects with a bidirectional or unidirectional relationship between them such that if one is updated then the other(s) may update also.

Notes:

  1. updated equals to - the former state of the object is no longer valid so a new object with different value(s) must be created (No setters; Functional style).

  2. I'm not looking for any kind of synchronization solutions.


Examples I created/found so far:

The notation B -> A means that B state may derive from A state and if A is updated then B may/may not update due to A update.

  1. unidirectional relationship: A download contains the following characteristics:

    • is download started: boolean - Can be set to true only once.
    • is currently downloading: boolean - Will be true when is download started is true. will be false whenis download completedwill befalse. Also, it may change fromtrue` to false and vice-versa by the user.
    • download speed: number - Will change as soon as is currently downloading is true
    • is download completed: boolean - Can be true iff is currently downloading is true.

relationships:

is download completed -> is currently downloading -> is download started and download speed -> is currently downloading

  1. bidirectional relationship: A master who's responsible for multiple slaves (jobs). A master and a slave contains the following characteristics:

master:

  • state: In progress/Canceled/Completed - It starts with In progress state and will be Completed iff all slaves have Completed state.
  • slaves: slave[]

slave:

  • state: In progress/Canceled/Completed - It starts with In progress state. Will be Canceled iff the master's state is Canceled and it states is In progress.

relationships:

master: state -> slaves: state, master: slave[] -> slaves: state and slave: state -> master: state.


Bonus: Example for a group of objects with a bidirectional relationship which can be changed to a unidirectional relationship after realizing there was a design problem - But isn't easy to see immediately.

3
  • You've already stipulated in your opening paragraphs that you will be using immutable objects, so why does any of this matter? – Robert Harvey May 10 '18 at 21:18
  • Good question - because if we request the state of each of them individually, meanwhile, there may be an update so I may get a group of results which, together, are invalid. There are solutions like Redux (which itself have some downsides) to solve this kind of problems. This question is about the problem and not the solution. Thanks – Stav Alfi May 10 '18 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.