I have a superclass called GameThread:
public class GameThread implements Runnable {
public static Thread thread;
public static boolean running = false;
public static double fps = 1;
public static double timePerTick = 1000000000 / fps;
public static ArrayList<GameThread> gameThreads = new ArrayList<>();
public void run(){
double delta = 0;
long now;
long lastTime = System.nanoTime();
long timer = 0;
while(running){
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
timer += now - lastTime;
lastTime = now;
setFps();
if(delta >= 1){
action();
delta--;
}
if(timer >= 1000000000){
timer = 0;
}
}
}
public synchronized void start(){
if (running) return;
running = true;
startThread();
thread.start();
}
public void action(){}
public void setFps(){}
public void startThread(){}
And 2 threads which are children of GameThread:
public class JumpThread extends GameThread {
public JumpThread(){
start();
}
@Override
public void action(){
//code
}
@Override
public void setFps(){
//code
}
@Override
public void startThread(){
thread = new Thread(new JumpThread());
}
and
public class HydroponicsThread extends GameThread {
public HydroponicsThread(){
start();
}
@Override
public void action(){
//code
}
@Override
public void setFps(){
//code
}
@Override
public void startThread(){
thread = new Thread(new HydroponicsThread());
}
My problem is, when I initialize them:
GameThread.gameThreads.add(new HydroponicsThread());
GameThread.gameThreads.add(new JumpThread());
Only the first one (HydroponicsThread) works, the "run()" method on the other won't be called. My guess is the while loop is stopping the JumpThread from being initialized, but I can't find a way to do this without it. Thanks in advance.