B4J Question Thread safe map error

LucaMs

Expert
Licensed User
Longtime User
A thread safe map, then inizialized with CreateThreadSafeMap, seems to not support GetKeyAt method.

java.lang.RuntimeException: java.lang.RuntimeException: Concurrent Map does not support this method.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
(I don't think I have to create a new thread for this, it seems to be related to the same thing)

Trying to get the values added to a "thread safe map":

MAIN
B4X:
Sub Process_Globals
    Public srvr As Server
    Public mapGameRooms As Map
End Sub

B4X:
Sub AppStart (Args() As String)
...
    mapGameRooms = srvr.CreateThreadSafeMap
    For r = 0 To 4
        Dim GameRoom As clsGameRoom
        GameRoom.Initialize
        Dim GameRoomName As String = "Room " & r
        GameRoom.Name = GameRoomName
        mapGameRooms.Put(GameRoomName, GameRoom)
    Next

    StartMessageLoop

End Sub


Websocket class
B4X:
Private Sub RunOnClient_SendRoomsList(Msg As String)

    Dim lst As List
    lst.Initialize

    Dim GameRoom As clsGameRoom

    For r = 0 To Main.mapGameRooms.Size - 1

        GameRoom = Main.mapGameRooms.Get(r)
        If GameRoom <> Null Then
            lst.Add(GameRoom.Name)
        Else
            Log("GameRoom is null")
        End If

    Next

    ws.RunFunction("FromServer_Rooms", lst)
    ws.Flush

End Sub

GameRoom is always Null
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Should you not use
B4X:
    GameRoomName = Main.mapGameRooms.GetKeyAt(r)
or
B4X:
    GameRoom = Main.mapGameRooms.GetValueAt(r)
Depending on if you want the name string or the actual room
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
The comments for Server.CreateThreadSafeMap make it clear that the Map created by this function doesn't support Map.GetValueAt and Map.GetKeyAt nor does it preserve the order of the elements.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The comments for Server.CreateThreadSafeMap make it clear that the Map created by this function doesn't support Map.GetValueAt and Map.GetKeyAt nor does it preserve the order of the elements.

I stand corrected, I was just going by the assist that pops up. Plus Lucas used ...get(r) so it could have been a typo.
 
Upvote 0
Top