Thread not starting

I am making a JFrame and for whatever reason my thread isn’t running

Here is my main: http://pastebin.java-gaming.org/3369f996c4319

And here is my Game: http://pastebin.java-gaming.org/369f90c73491f

As far as I see it should be running as it is pretty basic, but it is not. Does anyone see a reason why? I am not getting any errors and the JFrame is opening

Check your delta value in your game loop. It won’t be greater than one for a long time.

edit: never mind didnt see the “+=”.

Can you give a bit more information like does anything render and what is being printed to the console? Because when I run it on my computer the thread seems to be starting which makes me think it has something to do with your drawing and updating methods.

Nothing is rendering and I add something to print to the console everytime run() runs. Its like it isn’t even starting

EDIT: I looked over it again and it ran once then stopped

if main method is not blocking by [icode]game.start()[/icode] and the thread is daemon (https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#isDaemon--) then it’s terminated faster than started.

maybe that’s it. but your runnable code does not look like.

I had it print whether it was Daemon or not and it returned false

Why dont you add logging output to see where your program Flow goes? Or Single step debug your program?

I did and it ran the run() method once than quit

Yes but that is what should happen. Run is called once… You have to loop yourself, what you are doing already.

Did you want to ask why your program is terminating? I guess your if conditions arent proper.

Let me rephrase myself. The run() method starts the while loop and the while loop does not loop.
Here is what I got


public void run(){
    System.out.println(running);              //prints out true
    while(running) {
        System.out.println("Its running");   //Never prints out
        //Codey-stuff
    }
    System.out.println("Its over");           //Never prints out
}

Your last post shows code that should work fine, since you stripped it down very much. I ran your code in the ide, removed all calls to classes you didn’t provide and it ran as it should.

So the problem must lie somewhere else, but you didn’t provide a complete, runnable project, so we all have to guess what it could be. Since you already tried to single-step - you have placed a breakpoint on “while(running) {”, did you? If you only have one stop here…the variable isRunning is set to false somewhere in your code. There’s another problem in your code: the variable is not declared volatile, so (i guess) it’s not guaranteed that your game thread is ever informed about changes of the variable.

After more research I found the problem


	private void registerTiles(){
		for(int id = 0; id < 20; id++){
			for(int x = 0; x < width/WIDTH; x++){
				for(int y = 0; y < height/HEIGHT; y++){
					tiles.put(id, crop(ImageLoader.loadImage("/textures/ayraCity.png"), x*WIDTH, y*HEIGHT, width, height));
				}
			}
		}
	}

Appearantly this takes more time to load than expected. Sorry to bother you guys.

Why is the thread not running because of that?

@ndnwarrior15 the thread is starting, that code just makes the load time so long, it appears the thread does not start.

@kingroka123 I do suppose that a for-loopception would cause that

If you are using swing then you need to use EventQueue.invokeLater(myRunnable), this is because of how swing threading works and prevents Swing blocking or you blocking the swing thread.