I’m working atm with multithreading, but I’m having some issues with locks and I want to detect where are they. I’m using JMP as found on others topics, but it only shows how many time the lock (monitor state) was kept. I want to know how (even a tip is welcome) to see where on the code the lock is hapenning.
Then you should avoid ANY (with an exception) synchronization with other threads. Every lock means a waste of time for ‘realtime’ thread. The perfect situation is when your rendering thread reads messages from synchronized queue and, if it is needed, generates such messages and put them on other synchronized queues. For example - rendering thread is ‘stupid’, it only render, there is another thread which keeps the logic, when something needs to be done on the screen, the ‘logic’ thread sends a proper message to the rendering thread.
And that is what i’m doing (or at least trying), but to send messages to non-renderer thread, I need to use synchronize, which needs a lock (as far as I know).
For instance: I have two threads: The main one for rendering and another one to generate the meshes to be rendered (i’ll call it mesher). On the renderer thread I ask a “render” list from mesher thread and if the list isn’t empty, I render it. To avoid a long lock when asking for the list, I just copy it from mesher and release the lock:
But my question is if there is any way to know, using the JMP, where the “locking” code is being called. If I have just one call to this method, then is easy to know, but if I need to call it from other places (on renderer thread), then how I should know which one is having a long wait (to me, long wait is more then 10ms).
I don’t have a specific answer for your specific question sorry, but maybe you’ll find something interesting in RxJava and/or Akka as I did.
(see this [thread])
I personaly found a lot of solutions in RxJava. Coding becomes plumbering : managing the data flows through and between your subsystems.
And this is what you want to do : manage the flows between your mesher and your renderer.
: Multithreading approach for many AI - #30 by normen
On the renderer thread I ask a “render” list from mesher
So, it should be done by creating an message object and by placing it on the Mesher’s queue. mesher.put(message) should have an lock inside.
After that the Mesher take the message from its queue (inside an locked code ofc), generates the meshes, creates the reply message which contains the generated stuff (or its copy, if the Mesher needs to keep them) and put that message on the renderer’s queue (with synchronization ofc).
Then the renderer takes the message from its own queue (synchronized code ofc) and adds the meshes to the scene.
In my game it works very similar. imagine that a player clicks on the item on the ground.
message from renderer to the logic informs about click
the logic decides what to do (to take the object to player’s inventory)
th logic sends an message to renderer which contains the ID of the item to be removed from the scene
renderer reads the message and removes the item from the scene.
On the ‘logic side’ there is an object of Item class which contains the item’s geometry. if there is a need, the logic can send that geometry again to the scene.
@Empire_Phoenix I’ll give a try on synchronized HashMaps since it is built-in from Java (which means should be more optimal). But I didn’t understood why synchronized is bad on that example, you mean beacouse the overhead of clonning the HashMap to avoid concurrent issues?
Synchronized is the bad thing, no matter if you use locks or synchronized blocks or synchronized maps. Thats where you get your blocking from. People try to tell you that your threads should communicate via queues and not share objects they work on, thats bad threading.
Queues, and ConcurrentHashmap (they are not synchronized hashmaps, they work with more magic inside, i spend several hours understanding them ) are a better approach. synchronizing needs simplified to flush all cores l1,l2,l3 memorys to main memory. However you usually don’t need that at all.
I don’t think using sleep is a good aproach on thead loop, so I tought using this would be better…This is also a bad multithreading strategy? If so, how can I setup my thread loop w/o using sleep (which I think is worse).
You’d really do yourself a big favor if you became familiar with the java.util.concurrent package and did some reading on proper threading approaches. Some of the smartest threading guys in the world (and indeed input from many threading researches) put lots of effort into those classes.
I’ve read it already. I was so dumb that didn’t tought about it. Actually i’m using 4 queues, one for each state of my object, becouse this I was unable to use a ConcurrentBlockingQueue, but I can marge all those queues in a single one and just switch the action based on state
Anyway, thanks!! Today I’ve made really nice updates on my code and learned even more about Multithreading.