Simple Threading

Thx for all of the previous help that I've received form these forums I was wondering if u can assist me on my next issue that i've been attempting to solve for a couple weeks now

I'm having issues with threading. I have two threads the main one which I plan to do physics and ai on and the other one for rendering; however for some odd reason the update of the main thread is a lot faster than the openGL one and despite various attempts I can't seem to make the update speed of both the same. I've Tried the updateLocks and some jME code around standardGame to no prevail…Any suggestions

I wasn't attempting to Lock the thread per say however i've realized that the when I update the physics in the main thread as opposed to the openGL Thread the object either doesn't appear on screen or appears on screen for a short while(or if I constrain the physics update to a key it shows until i press the key) before disappearing. in the openGL thread it falls slowly downward.

as it stands


package util;
 
import java.util.logging.Level;
 
import com.jme.util.NanoTimer;
import com.jme.util.Timer;
import com.jmex.game.StandardGame.GameType;
 
public class Manager {
   /**High resolution timer. remember that tpf is .getTimePerFrame();*/
   private final Timer timer = (gameSingleton.game.getGameType() == GameType.GRAPHICAL)? Timer.getTimer(): new NanoTimer();;
 
   
   public Manager(){
      
         gameSingleton.get().getPhysics().build(); //Build octrees (may take a while)
         System.out.println("Collision-Scene Built Sucesffully!!");
      run();
         System.out.println("Thread is ending");   
   }
   
   private void run() {
      try {
         
      while (gameSingleton.game.isStarted()&&!gameSingleton.get().getDisplay.isClosing()){
         
         
         mainUpdate(-1);
         
         Thread.yield();
      }
   } catch (Throwable t) {
      gameSingleton.getLogger.logp(Level.SEVERE, this.getClass().toString(), "start()", "Exception in game loop", t);
      }
   }
   public void mainUpdate(float tpf){
      gameSingleton.get().mainUpdate(tpf);
      ObjectManager.get().mainUpdate(tpf);      
   }
}

I tried adding an if statement around the mainUpdate within the while loop with a boolean set to false that was updated true in the standardGame update method. Though it seems to work it tends to freeze at times

Your code makes no sense to me. It looks like a single-thread application. Are you sure you understood how threads work in Java? When not try to implement for example the dining philosophers problem.



I would suggest to write down in simple pseudo-code what you want to do.

To avoid threading-problems you have to understand what your threads are doing.

You have to know which tasks can be performed parallel and when you should sync the data.

Working with more than one thread on the same data will lead to undefined states and is also slower, than a clean sync.

JackNeil said:

Your code makes no sense to me. It looks like a single-thread application. Are you sure you understood how threads work in Java? When not try to implement for example the dining philosophers problem.


I use this class with standard Game but i separated it into another class called Manager in which i simply call new Manager at the end of the main method. I did this because before i took a lot of things out my main class was getting kind of long and wanted to focus on only this aspect. I plan to add the successful class back to the main class once I figure it out.
The main class now only really initiates standardgame(is the second thread I was referring to I apologize for not adding it to the original post instead of saying thread for rendering) a couple of gameStates and then the above class

as for main update instead of creating a new timer i made the class call gameState, attached it, declared a float, udpated it in the update method which is from the main game loop in the standardGames thread and added the value as the tpf to mainUpdate. I then surrounded the mainUpdate method with an if statement the boolean was updated as false in the end but true in the gameState update method which meant that mainUpdate only updated when standardGame udpated.
This seems to be the only way that marginally works for me, though i'm obviously still a newb and thier are obviously a lot better ways to do it.

Are their any other suggestions I might be able to look into