Jump to content
  • Advertisement
Sign in to follow this  
Caste

[java] Rendering in extra thread works for a while only?

This topic is 3878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm encountering a strange problem with Threads in Java: We're developing a complete Game Engine in Java using LWJGL, the main thread is our physics engine as it controls everything, and every now and then it calls a render thread. The physics engine has a Renderer-instance which is derived from Runnable, the run method invokes the rendering of a new frame. If the physics engine notices that it's time to render again it calls:
if (!renderThread.isAlive()) {
  //some setup here
  renderThread = new Thread(renderer);
  renderThread.start();
}
This works fine for a while on my machine, but then it just stops rendering, the main thread keeps running. Is there kind of a limitation for the number of threads? Another thing which makes me wonder even more is that for me (running on an Intel Core 2 Duo 2GHz - Ubuntu Gutsy) using a render thread does not only make it crash at some point, but also is slower and less smooth than without threads. Whereas a mate from our team (MacBook, also dual core with 2GHz) gains a lot of performance using exactly this code for threading. Any hint whats the matter there? Thx a lot

Share this post


Link to post
Share on other sites
Advertisement
IMHO it would be better if you would keep the thread running, you have a lot of overhead if you start a new thread, this is not something you want to do a couple of times every second. You could have a "dirty flag" which tells the renderer it should render again, otherwise it could sleep for a while and check if it should render again. Something like


while(!quit)
{
if (dirty)
{
Render();
}
else
{
Thread.Sleep(20);
}
}

Share this post


Link to post
Share on other sites
Hmm I tried that (and another hint using a while-loop and notify) but both didnt do the trick.

I dont understand why the thread calling renderThread.start() does not continue after the first time it rendered, so the renderThread keeps waiting/sleeping forever and the other one does not continue..

Share this post


Link to post
Share on other sites
Concurrency is a hard way to do thinks.

The java tutorials should help you (http://java.sun.com/docs/books/tutorial/)


I am doing something close, and I've found some problems. I resolved them grouping EVERY render code on the SAME thread.



The flag think...

every "wait" must be in a synchronized function/block in java.



synchronized run(){
while(!quit)
{
while(!dirty)
wait();

render();
}
}





synchronized change_flag(){
dirty = true;
//you need to inform that the flag has changed
notifyAll();
}



concurrency, as I said, is hard stuff. You need to deal with deadlock and other nasty thinks

Share this post


Link to post
Share on other sites
Still same thing, exactly the same as I tried before, only that now the notify is inside the renderer and not from ouside calling renderThread.notify();

Every rendering happens in the renderThread, it just gets position updates from the physics thread. I made all functions passing data synchronized, but still the wait() inside my run()-method waits forever.

Thx anyways, guess I need to dig some deeper into this topic.

Share this post


Link to post
Share on other sites
Could you post the code where it's doing the wait and where it's doing the notify? Seems like you might have a race condition.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!