B4J Question [Server] TheadSafeMap and locks

Alessandro71

Well-Known Member
Licensed User
Longtime User
some questions about ThreadSafeMap in a jServer environment:

  1. a Map that is loaded once with CreateMap(...) at startup and is used only with Get, does not need to be a ThreadSafeMap, correct?
  2. a Map created in a ServerHandler Class_Globals, and used only within that class, does not need to be a ThreadSafeMap, correct?
  3. a Map in a Process_Globals, accessed with Get and Put should be a ThreadSafeMap, correct?
  4. a Map that should be a ThreadSafeMap, but is erroneously declared as Map, can cause an exception at runtime or just unpredictable results because of eventual concurrent write access?
I'm fairly positive about 1, 2, and 3, but in doubt about 4, and would like some feedback from users more experienced with the server environment.

and last but not least, what is the correct way to obtain a Lock on a ThreadSafeMap for a read/modify/write operation?
 

LucaMs

Expert
Licensed User
Longtime User
I'm not a great expert, but I agree with you on the first three points.
For point 4, which is related to the last part of your question, I'd say you should prevent direct access to that Map, meaning you should create a Subroutine to manage it (which is the same difference between creating a public variable instead of creating the Subroutines setSomething and / or getSomething to protect the variable, not allowing invalid values to be assigned to it).
 
Last edited:
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
I don't think a Sub is enough to serialize concurrency, but I'm more than willing to be proven wrong
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
In B4J, a Java-based development environment, there is no specific class called ThreadSafeMap like in standard Java, but synchronization of access to a Map can be managed to make it thread-safe using manual synchronization techniques typical of B4J or Java.

A common option is to use a regular Map object and synchronize its access with critical blocks using the synchronized keyword or locks (for example, using the JavaObject class to access Java synchronization if necessary). Another simpler approach in B4J is to use your Map object within a Synchronized object using the Sync statement to protect sections of code that access or modify the Map, preventing uncontrolled concurrent access.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
In B4J, a Java-based development environment, there is no specific class called ThreadSafeMap like in standard Java, but synchronization of access to a Map can be managed to make it thread-safe using manual synchronization techniques typical of B4J or Java.

A common option is to use a regular Map object and synchronize its access with critical blocks using the synchronized keyword or locks (for example, using the JavaObject class to access Java synchronization if necessary). Another simpler approach in B4J is to use your Map object within a Synchronized object using the Sync statement to protect sections of code that access or modify the Map, preventing uncontrolled concurrent access.
This looks like an AI generated answer, totally out of context here
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
This looks like an AI generated answer, totally out of context here
See:
and
In Java, ConcurrentHashMap is a class that provides a thread-safe implementation of a map, similar to the idea of ThreadSafeMap. ConcurrentHashMap allows multiple threads to access and modify the map simultaneously, without requiring the entire data structure to be locked, as a conventional Hashtable would.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I would guess it uses java.util.concurrent.ConcurrentHashMap so is safe on read and writes
 
Upvote 0
Top