add a little extra safety to ensure the updater thread stops on dispose

This commit is contained in:
Gered 2014-04-16 07:54:51 -04:00
parent 75691fa357
commit da47617e9f
2 changed files with 10 additions and 2 deletions

View file

@ -15,6 +15,7 @@ public class TileMap extends TileContainer implements Disposable {
final Vector3 tmpPosition = new Vector3(); final Vector3 tmpPosition = new Vector3();
TileMapUpdater updater; TileMapUpdater updater;
Thread updaterThread;
public final int chunkWidth; public final int chunkWidth;
public final int chunkHeight; public final int chunkHeight;
@ -149,6 +150,7 @@ public class TileMap extends TileContainer implements Disposable {
bounds.max.set(getWidth(), getHeight(), getDepth()); bounds.max.set(getWidth(), getHeight(), getDepth());
updater = new TileMapUpdater(this); updater = new TileMapUpdater(this);
updaterThread = null;
} }
public void updateVertices() { public void updateVertices() {
@ -160,7 +162,8 @@ public class TileMap extends TileContainer implements Disposable {
if (isUpdating()) if (isUpdating())
throw new IllegalStateException("Async vertices update for this TileMap is currently underway."); throw new IllegalStateException("Async vertices update for this TileMap is currently underway.");
(new Thread(updater)).start(); updaterThread = new Thread(updater);
updaterThread.start();
} }
public boolean updateVerticesAsync() { public boolean updateVerticesAsync() {
@ -294,6 +297,7 @@ public class TileMap extends TileContainer implements Disposable {
@Override @Override
public void dispose() { public void dispose() {
updater.signalStop();
for (int i = 0; i < chunks.length; ++i) for (int i = 0; i < chunks.length; ++i)
chunks[i].dispose(); chunks[i].dispose();
} }

View file

@ -7,6 +7,7 @@ public class TileMapUpdater implements Runnable {
TileChunk chunkNeedingVboCreation; TileChunk chunkNeedingVboCreation;
float progress; float progress;
boolean isRunning; boolean isRunning;
boolean needToStop;
public TileMapUpdater(TileMap tileMap) { public TileMapUpdater(TileMap tileMap) {
this.tileMap = tileMap; this.tileMap = tileMap;
@ -32,14 +33,17 @@ public class TileMapUpdater implements Runnable {
waitingForVboCreation = false; waitingForVboCreation = false;
} }
public synchronized void signalStop() { needToStop = true; }
@Override @Override
public void run() { public void run() {
isRunning = true; isRunning = true;
chunkNeedingVboCreation = null; chunkNeedingVboCreation = null;
needToStop = false;
TileChunk[] chunks = tileMap.getChunks(); TileChunk[] chunks = tileMap.getChunks();
for (int i = 0; i < tileMap.chunks.length; ++i) { for (int i = 0; (i < tileMap.chunks.length && !needToStop); ++i) {
progress = (float)i / (float)(chunks.length - 1); progress = (float)i / (float)(chunks.length - 1);
TileChunk chunk = chunks[i]; TileChunk chunk = chunks[i];