create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / startup / ConcurrentLoadingThrustCurveMotorSetDatabase.java
index a5f6006b990c8da5b0f89f7c33cc58e20a68fa58..2eba9b426206018e08596b273a8f633f70b731b6 100644 (file)
@@ -5,14 +5,16 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+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;
 import net.sf.openrocket.file.iterator.DirectoryIterator;
 import net.sf.openrocket.file.iterator.FileIterator;
-import net.sf.openrocket.file.motor.GeneralMotorLoader;
 import net.sf.openrocket.file.motor.MotorLoaderHelper;
 import net.sf.openrocket.gui.util.SimpleFileFilter;
 import net.sf.openrocket.gui.util.SwingPreferences;
@@ -46,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
@@ -57,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();
 
@@ -122,9 +139,26 @@ public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMot
 
                private BookKeeping() {
 
-                       writerThread = Executors.newSingleThreadExecutor();
-
-                       loaderPool = Executors.newFixedThreadPool(25);
+                       writerThread = new ThreadPoolExecutor(1,1,200, TimeUnit.SECONDS,
+                                       new LinkedBlockingQueue<Runnable>(),
+                                       new ThreadFactory() {
+                               @Override
+                               public Thread newThread(Runnable r) {
+                                       Thread t = new Thread(r,"MotorWriterThread");
+                                       return t;
+                               }
+                       });
+
+                       loaderPool = new ThreadPoolExecutor(10,10, 2, TimeUnit.SECONDS,
+                                       new LinkedBlockingQueue<Runnable>(),
+                                       new ThreadFactory() {
+                               int threadCount = 0;
+                               @Override
+                               public Thread newThread(Runnable r) {
+                                       Thread t = new Thread(r,"MotorLoaderPool-" + threadCount++);
+                                       return t;
+                               }
+                       });
 
                        workGenerator = new WorkGenerator();
 
@@ -144,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();