From f94a7439cb9f2913a26ddfa66154defb8895533c Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Mon, 7 May 2012 14:43:50 +0000 Subject: [PATCH] Multithread the orc loading process. git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@653 180e2498-e6e9-4542-8430-84ac67f01cd8 --- .../net/sf/openrocket/startup/Startup.java | 8 --- .../net/sf/openrocket/startup/Startup2.java | 50 +++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/core/src/net/sf/openrocket/startup/Startup.java b/core/src/net/sf/openrocket/startup/Startup.java index e816a11a..31f83184 100644 --- a/core/src/net/sf/openrocket/startup/Startup.java +++ b/core/src/net/sf/openrocket/startup/Startup.java @@ -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. diff --git a/core/src/net/sf/openrocket/startup/Startup2.java b/core/src/net/sf/openrocket/startup/Startup2.java index 11ceb190..7c2ed95a 100644 --- a/core/src/net/sf/openrocket/startup/Startup2.java +++ b/core/src/net/sf/openrocket/startup/Startup2.java @@ -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 -- 2.47.2