Преглед на файлове

TerrainGrid executor now prints out any exceptions from the threads

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9776 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
bre..om преди 13 години
родител
ревизия
7f740a0cd4
променени са 1 файла, в които са добавени 37 реда и са изтрити 3 реда
  1. 37 3
      engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java

+ 37 - 3
engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java

@@ -46,13 +46,20 @@ import com.jme3.terrain.Terrain;
 import com.jme3.terrain.heightmap.HeightMap;
 import com.jme3.terrain.heightmap.HeightMapGrid;
 import java.io.IOException;
+import java.lang.Thread.UncaughtExceptionHandler;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -458,15 +465,42 @@ public class TerrainGrid extends TerrainQuad {
         return terrain.getMaterial(worldLocation);
     }
 
+    /**
+     * This will print out any exceptions from the thread
+     */
     protected ExecutorService createExecutorService() {
-        return Executors.newSingleThreadExecutor(new ThreadFactory() {
+        final ThreadFactory threadFactory = new ThreadFactory() {
             public Thread newThread(Runnable r) {
                 Thread th = new Thread(r);
-                th.setName("jME Terrain Thread");
+                th.setName("jME TerrainGrid Thread");
                 th.setDaemon(true);
                 return th;
             }
-        });
+        };
+        ThreadPoolExecutor ex = new ThreadPoolExecutor(1, 1,
+                                    0L, TimeUnit.MILLISECONDS,
+                                    new LinkedBlockingQueue<Runnable>(), 
+                                    threadFactory) {
+            protected void afterExecute(Runnable r, Throwable t) {
+                super.afterExecute(r, t);
+                if (t == null && r instanceof Future<?>) {
+                    try {
+                        Future<?> future = (Future<?>) r;
+                        if (future.isDone())
+                            future.get();
+                    } catch (CancellationException ce) {
+                        t = ce;
+                    } catch (ExecutionException ee) {
+                        t = ee.getCause();
+                    } catch (InterruptedException ie) {
+                        Thread.currentThread().interrupt(); // ignore/reset
+                    }
+                }
+                if (t != null)
+                    t.printStackTrace();
+            }
+        };
+        return ex;
     }
     
     @Override