create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / startup / ConcurrentLoadingThrustCurveMotorSetDatabase.java
index 3f6e9fcbb9190b74a182487eb4685df4304f3cd2..2eba9b426206018e08596b273a8f633f70b731b6 100644 (file)
@@ -5,15 +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;
@@ -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();
 
@@ -123,7 +139,9 @@ public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMot
 
                private BookKeeping() {
 
-                       writerThread = Executors.newSingleThreadExecutor( new ThreadFactory() {
+                       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");
@@ -131,7 +149,9 @@ public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMot
                                }
                        });
 
-                       loaderPool = Executors.newFixedThreadPool(25, new ThreadFactory() {
+                       loaderPool = new ThreadPoolExecutor(10,10, 2, TimeUnit.SECONDS,
+                                       new LinkedBlockingQueue<Runnable>(),
+                                       new ThreadFactory() {
                                int threadCount = 0;
                                @Override
                                public Thread newThread(Runnable r) {
@@ -158,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();