]> git.gag.com Git - debian/openrocket/commitdiff
Multithread the orc loading process.
authorkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 7 May 2012 14:43:50 +0000 (14:43 +0000)
committerkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 7 May 2012 14:43:50 +0000 (14:43 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@653 180e2498-e6e9-4542-8430-84ac67f01cd8

core/src/net/sf/openrocket/startup/Startup.java
core/src/net/sf/openrocket/startup/Startup2.java

index e816a11a8d2c2a50dbb2038ddba0fc0923420c42..31f83184ad18c4026973e4f2d3919ce2fc0f0bfb 100644 (file)
@@ -4,7 +4,6 @@ import java.io.PrintStream;
 import java.util.Locale;
 import java.util.prefs.Preferences;
 
-import net.sf.openrocket.database.ComponentPresetDatabase;
 import net.sf.openrocket.gui.util.SwingPreferences;
 import net.sf.openrocket.l10n.DebugTranslator;
 import net.sf.openrocket.l10n.L10N;
@@ -39,7 +38,6 @@ public class Startup {
        
        private static final int LOG_BUFFER_LENGTH = 50;
        
-       
        /**
         * OpenRocket startup main method.
         */
@@ -56,17 +54,11 @@ public class Startup {
                // Setup the translations
                initializeL10n();
 
-               // Must be done after localization is initialized
-               ComponentPresetDatabase componentPresetDao = new ComponentPresetDatabase();
-               componentPresetDao.load("datafiles/presets", "(?i).*orc");
-               Application.setComponentPresetDao( componentPresetDao );
-               
                // Continue startup in Startup2 class (where Application is already set up)
                Startup2.runMain(args);
                
        }
        
-       
 
        /**
         * Set proper system properties if openrocket.debug is defined.
index 11ceb19049f523e7a7741d09c61c664309d205f1..7c2ed95ab860a1e56aca23c24e509151927e2318 100644 (file)
@@ -5,6 +5,11 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.File;
 import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.swing.SwingUtilities;
@@ -13,6 +18,7 @@ import javax.swing.ToolTipManager;
 
 import net.sf.openrocket.communication.UpdateInfo;
 import net.sf.openrocket.communication.UpdateInfoRetriever;
+import net.sf.openrocket.database.ComponentPresetDatabase;
 import net.sf.openrocket.database.Databases;
 import net.sf.openrocket.database.ThrustCurveMotorSet;
 import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
@@ -90,6 +96,25 @@ public class Startup2 {
                log.info("Initializing the splash screen");
                Splash.init();
                
+               // Latch which counts the number of background loading processes we need to complete.
+               CountDownLatch loading = new CountDownLatch(1);
+               ExecutorService exec = Executors.newFixedThreadPool(2, new ThreadFactory() {
+
+                       @Override
+                       public Thread newThread(Runnable r) {
+                               Thread t = new Thread(r);
+                               t.setPriority(Thread.MIN_PRIORITY);
+                               return t;
+                       }
+                       
+               });
+
+               // Must be done after localization is initialized
+               ComponentPresetDatabase componentPresetDao = new ComponentPresetDatabase();
+               exec.submit( new ComponentPresetLoader( loading, componentPresetDao));
+               
+               Application.setComponentPresetDao( componentPresetDao );
+               
                // Setup the uncaught exception handler
                log.info("Registering exception handler");
                SwingExceptionHandler exceptionHandler = new SwingExceptionHandler();
@@ -122,6 +147,12 @@ public class Startup2 {
                loadMotor();
                Databases.fakeMethod();
                
+               try {
+                       loading.await();
+               } catch ( InterruptedException iex) {
+                       
+               }
+
                // Starting action (load files or open new document)
                log.info("Opening main application window");
                if (!handleCommandLine(args)) {
@@ -270,6 +301,25 @@ public class Startup2 {
                timer.start();
        }
        
+       private static class ComponentPresetLoader implements Callable {
+
+               CountDownLatch latch;
+               ComponentPresetDatabase componentPresetDao;
+               
+               private ComponentPresetLoader( CountDownLatch latch, ComponentPresetDatabase componentPresetDao ) {
+                       this.componentPresetDao = componentPresetDao;
+                       this.latch = latch;
+               }
+               
+               @Override
+               public Object call() throws Exception {
+                       componentPresetDao.load("datafiles/presets", "(?i).*orc");
+                       latch.countDown();
+                       return null;
+               }
+
+       }
+       
        /**
         * Handles arguments passed from the command line.  This may be used either
         * when starting the first instance of OpenRocket or later when OpenRocket is