Threads hanging themselves.

So, I’m making a planet generator. It builds it’s continents with perlin noise, and then I run an algorithm I wrote to map out global temperatures based on some photos I viewed of earth’s average global temperatures.

So other than that algorithm being pretty bad most of the time, I’ve got another problem that’s coming from Java’s threads.

At the end of the standard generation I listed above, I must begin landmark and organism generation and placement. This proved to be very CPU extensive, so I thought it’d be a good idea to make a thread for it instead of trying to execute it all in one step to keep the program from freezing up.

So, essentially, it moves across the map, generating a local map for every tile on a 128x128 map. Each local map is 32x32 tiles. Not too bad, right?

Anyway, it runs, and then around at a y pf 55-59 it stops completely, as in it just stops looping. No errors fire, no exceptions, no crashes, the game runs fine. What this does however, is keep the generator from progressing, which is the problem. Honestly, I have no idea what’s going on, I’ve never had this problem with a thread before.

Here’s the code, not that this is the “alpha” code, and has yet to be cleaned up for max efficiency (don’t laugh!):

	Thread threadGenerator = new Thread(){
		public void run(){
						//Local Maps
			for (int x = 0; x < planetSize; x++){
				for (int y = 0; y < planetSize; y++){
					System.out.println(x+"/"+y);
			
					if (map[x][y][0] != TILE_WATER && map[x][y][0] != TILE_RIVER && map[x][y][0] != TILE_MOUNTAIN){
						if (map[x][y][1] > 55){
							if (r.nextInt(2000) < 10){
								map[x][y][0] = TILE_TOWN;
								landmarks++;
							}else if (r.nextInt(5000) < 10){
								map[x][y][0] = TILE_TOWN_RUINS;
								landmarks++;
							}
						}
								
						if (r.nextInt(1500) < 20){
							map[x][y][0] = TILE_CAVE;
							landmarks++;
						}
					}
							
					localMaps[x][y] = new LocalMap(map[x][y][1], map[x][y][0]);
					localMaps[x][y].generateObjects(r, organisms);
					population+=localMaps[x][y].getLocalPopulation();
							
					if (population > maxPopulation && limitPopulation){
						localMaps[x][y].clearPopulation();
						population = maxPopulation;
					}
				}
			}
		};

Any ideas? I’m dumbfounded.

The thead looks kinda odd, using a lot of data outside the scope.
Didnt even know this would really work.
For my planet generation i made a class to do alot of diffrent stuff (implements Runnable).
You just pass some parameters like planet size and leave it alone until its done.

But about the error, finished = true; probably gets triggered, why dont you use a for loop for this, much cleaner i guess.
And these lines are still inside the loop and get triggered 55-59 times, is this supposed to?

done = true;
System.out.println(“Done”);

I dont know what you do on done = true, but maybe another thread stops it then?

Without submerging myself into the code, I suggest either using a debugger or a a profiler. Debugger can show You the internals of a infinite loop, it can help You determine, why the loop doesn’t end.
Profiler will help You, if You have a threadlock.

A debugger comes with any of the big IDEs (eclipse, idea and netbeans).
A profiler comes with the JDK (atleast with oracle jdk), under bin folder with the name “jvisualvm”

Debugger and a profiler is pretty much “must know” tools. They are easy enough to use, google will help You.

try to make the boolean value volatile, I find that fixes alot of problems in my threads! (then again, I don’t use threads much anyway). Its like a programmers trap filled with nightmares.

If you don’t know why, don’t screw around with random fixes. In this case, it’s a wildly inappropriate mechanism. First, Thread has its own completion flag built right in. Second, if you want to take action or wait on the completion of a thread, you should be using java.util.concurrent.

In fact I’d go so far as to say if you start out with concurrency by typing T-h-r-e-a-d you are doing it wrong

So you so that “extends Thread” is bad and “implements Runnable” would be ok ?

Edit: ah Runnable is not from java.util.concurrent, I guess I have never used that package either than. I only do concurrency rarely but I would use Runnable and make a new thread with a runnable instance as constructor parameter…

java.util.concurrent is all about Executors and … stuff, you know? ;D

Nope, I mean working with java.lang.Thread directly at all. Obviously at some point you may make use of Thread, but my little epistle was about starting off with it, which is something you don’t do any more than you should be starting off your programs by optimizing an opcode dispatch jump table for page locality. “implements Runnable” is fine, best passed to an ExecutorService to get a Future back, not Thread. Presto, free thread pooling. Pass it a callable instead and now you can get results back. j.u.c. is full of all kinds of goodies, and it’s just the start of the high-level concurrency stuff that other libraries build on top of it.

I’m going to check out runnable, but I’ll also read up on concurrent like you said I should. The code looks nasty because it’s basically a rough draft. Usually I try to make it work, and then I clean it up so that it looks pretty. Just how I am, Hahah.

It works great until it just stops itself. That’s the thing, it’s supposed to get triggered around 128 times but stops about halfway through for no apparent reason.

So why dont you use for(int x = 0; x < planetSize; x++){ }
I dont understand whou your using a while loop, it makes your code alot less readable.

Maybe it stops because you have x < planetsize-1 and y < planetsize-1, you skipped the last row of the x and y.
your code just creates confusion.

Yeah, I fixed it by doing this.
I’m really inexperienced with using Threads instead of just While statements, so my dumbass thought a while statement was a must in a Thread.

I can program neural networks to learn how to speak but I can’t use a thread, I feel so ignorant! Hahah

Edit:
I’m still getting hangs. I’ve implemented runnable and tried that as well. It still stops about a quarter in.
I’m going to try concurrent and see what that does.

Edit, Edit:
I used executorService, and it hangs too. :frowning:

After debugging to see where the thread was deadlocking, I arrived at this point inside the local map generator.

			if (mapType == MAP_TOWN){
				int tries = 0;
				ArrayList<House> houses = new ArrayList<House>();
				int maximumHouses = r.nextInt(MAXIMUM_HOUSES);
				
				while(houses.size() < maximumHouses && tries < MAXIMUM_HOUSE_PLACE_FAILS){
					House house = new House(r.nextInt(32), r.nextInt(32), 4 + r.nextInt(16), 4 + r.nextInt(16));
					
					for (int a = 0; a < houses.size(); a++){
						if (house.intersectWith(houses.get(a))){
							tries++;
							break;
						}
					}

						houses.add(house);
				}
				
				for (int a = 0; a < houses.size(); a++){
					map = houses.get(a).placeHouse(map);
				}
			}

According to the debugger, the locked thread had stopped at this point:

House house = new House(r.nextInt(32), r.nextInt(32), 4 + r.nextInt(16), 4 + r.nextInt(16));

So, is it Random that’s killing everything? It’s odd because it generates a few houses before this point generally and the House constructor just sets its values to the ones passed in. I’ve tried slowing down the thread and it still inevitably freezes over.

Have you tried removing the randoms?

Yes, and it works fine when I do.

Edit: I managed to fix it. It was a problem with my loop. I replaced the one above with the one that works.

It seemed that Java wasn’t breaking at the right spot, so the loop itself turned into an indefinite loop, holding up the Thread.