Tweak the pool sizes and allow for longer time to load things. This improves reliabi...
[debian/openrocket] / core / src / net / sf / openrocket / startup / ConcurrentLoadingThrustCurveMotorSetDatabase.java
index a546f739e55ff2c5ccec5b636676c4cabdc27635..2eba9b426206018e08596b273a8f633f70b731b6 100644 (file)
@@ -9,6 +9,7 @@ import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import net.sf.openrocket.database.ThrustCurveMotorSet;
 import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
@@ -47,6 +48,9 @@ public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMot
        private static final LogHelper log = Application.getLogger();
        private final String thrustCurveDirectory;
 
+       /** Block motor loading for this many milliseconds */
+       // Block motor loading for 1.5 seconds to allow window painting to be faster
+       private static AtomicInteger blockLoading = new AtomicInteger(1500);
 
        public ConcurrentLoadingThrustCurveMotorSetDatabase(String thrustCurveDirectory) {
                // configure ThrustCurveMotorSetDatabase as true so we get our own thread in
@@ -58,6 +62,18 @@ public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMot
        @Override
        protected void loadMotors() {
 
+               // Block loading until timeout occurs or database is taken into use
+               log.info("Blocking motor loading while starting up");
+               /*
+               while (!inUse && blockLoading.addAndGet(-100) > 0) {
+                       try {
+                               Thread.sleep(100);
+                       } catch (InterruptedException e) {
+                       }
+               }
+               */
+               log.info("Blocking ended, inUse=" + inUse + " blockLoading=" + blockLoading.get());
+
                BookKeeping keeper = new BookKeeping();
                keeper.start();
 
@@ -133,7 +149,7 @@ public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMot
                                }
                        });
 
-                       loaderPool = new ThreadPoolExecutor(25,25, 2, TimeUnit.SECONDS,
+                       loaderPool = new ThreadPoolExecutor(10,10, 2, TimeUnit.SECONDS,
                                        new LinkedBlockingQueue<Runnable>(),
                                        new ThreadFactory() {
                                int threadCount = 0;
@@ -162,9 +178,9 @@ public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMot
                private void waitForFinish() throws InterruptedException {
                        try {
                                loaderPool.shutdown();
-                               loaderPool.awaitTermination(10, TimeUnit.SECONDS);
+                               loaderPool.awaitTermination(90, TimeUnit.SECONDS);
                                writerThread.shutdown();
-                               writerThread.awaitTermination(10, TimeUnit.SECONDS);
+                               writerThread.awaitTermination(90, TimeUnit.SECONDS);
                        }
                        finally {
                                iterator.close();