Multithreaded loading

Well as the comparision says, JME3 is able to load models in a sepreate thread, so my main question is what do I have to take care of when doing it this way?



My idea is just that I have some kinda  of background loader that I can pass a modelname + a node that should it be attached. Then the game continues while the Background loads the model, once finished it should be added to the node passed.



Possible problems I see

  1. Loading textures/shaders for the model might result in problems (at least it did in JME2 it should work now i guess)
  2. Attaching the child out of another Thread directly into the node might cause problems if it is rendering at the smae time?
  3. The first frame the model appears a lag happens when the textures are uploaded to the opengl vram.



    Any tips, ideas would be appreciated

Yep, the TextureManager is one of the problems in my multi-displaysystem jme2 implementation (which is running btw, but still has a bunch of potential thread-safety issues due to statics everywhere).



I think that one way to solve the problem is to use threadlocals inside the TextureManager, or use one TextureManager for each DisplaySystem.



I would really like to switch all my current JME2 based work (3D broadcast video engine) to JME3 but I feel it's a bit too fresh. I don't know.

I was able to load models with textures etc. on a different thread in jme2 already..

You can load textures from another thread, but you can't load them from multiple threads simultaneously, since TextureManager is not thread-safe in jME2.
I have recently commented out the multithreaded-based loading in jME3, it became incompatible to the many changes that happened with asset handling. The best way to do it is probably to have a preload list, that tells all resources that shall be loaded, then these resources are loaded in multiple threads depending on how many cores the machine has, you would then receive notifications in the OpenGL thread telling you that a resource has finished loading and needs handling.

I was able to load models with textures etc. on a different thread in jme2 already… The attaching to the scenegraph might be something you want to do on the OpenGL thread, but loading, I think its no problem at all…

MikOfClassX said:

I would really like to switch all my current JME2 based work (3D broadcast video engine) to JME3 but I feel it's a bit too fresh. I don't know.

Yes, jme3's structure is still not set in stone, so basing a project on it right now is not really recommended. But if you try around with it.. any feedback is very welcome and will help get jme3 out sooner.

Can someone give me an example of loading models by multithread ? Since i’m really not familiar with the multithreading programmation it seems to be very important on complex game in JME like what i have …

Well actually just do the loadmodel path in a second thread, and then attach it in the opengl thread.

I strongly suggest learning a bit more about threading and synchronizing however before as you will make mistakes otherwise.



If you can read german I could give you some stuff where it is explained, other wise you have to search yourself

i can’t read german >< do you have any example to give me ?

i’m doing a 3D RPG and i’m pretty sure that i have to program on multi thread i really need to know how it works in JAVA please and especially with JME3

:google: java concurrency

OK i found the informations about it and know how it works in Java ( i knew throught the school btw lol ). How are they used to be optimized in JME3?



Can someone post me a part of his code in a project he made so i can see how it works ?



Do i need for example to do a Thread for loading assets another thread for loading terrain another thread for AI etc … ?

Or am i completely out of the line here ?

You can assetManager.loadModel() on a separate thread, then you only have to attach the model on the OpenGL thread like this:

[snippet id=“10”]

And how do you assetManager.loadModel() on a separate thread ?

When i attach to OpenGl Thread what does it invovled ? Does it mean it will work at the begining of the compilation ? Or is there a way to control the thread and tell it to execute only at this time ? Is that even possible to makes priority thread ?

[java]



public class AssetLoader extends Thread

{

private AssestManager am;



public AssetLoader(AssestManager assetm)

{

am = assetm;

}

public void run()

{

am.loadModel() // replace with real code

}



}

[/java]



Then in your game code

[java]

public mygame extends SimpleApplication

{



AssetLoader al = new AssetLoader(assetmanager);

al.start();



}

[/java]

1 Like

I see , so if i want to use a Thread i must create a class that extends Thread ?



Thank you for the example it’s more clear like that

thats not the only way to make new threads. you really need to read up on the subject.

http://www.javaworld.com/jw-04-1996/jw-04-threads.html

I’ll do it thank you

danath said:
OK i found the informations about it and know how it works in Java ( i knew throught the school btw lol ).

Why do you say you know how it works if you dont??

I know for Java but i thought that JME was something else that’s all

danath said:
I see , so if i want to use a Thread i must create a class that extends Thread ?

danath said:
And how do you assetManager.loadModel() on a separate thread ?

Yeah, sure.. :roll:
1 Like

haha Normen quit trolling him, you are flooding my inbox :wink: