1 package net.sf.openrocket.startup;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ThreadFactory;
10 import java.util.concurrent.TimeUnit;
12 import net.sf.openrocket.database.ThrustCurveMotorSet;
13 import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
14 import net.sf.openrocket.file.iterator.DirectoryIterator;
15 import net.sf.openrocket.file.iterator.FileIterator;
16 import net.sf.openrocket.file.motor.GeneralMotorLoader;
17 import net.sf.openrocket.file.motor.MotorLoaderHelper;
18 import net.sf.openrocket.gui.util.SimpleFileFilter;
19 import net.sf.openrocket.gui.util.SwingPreferences;
20 import net.sf.openrocket.logging.LogHelper;
21 import net.sf.openrocket.motor.Motor;
22 import net.sf.openrocket.motor.ThrustCurveMotor;
23 import net.sf.openrocket.util.BugException;
24 import net.sf.openrocket.util.Pair;
27 * Load motors in parallel using a three stage pipeline.
29 * Stage 1: single thread managed by the ThrustCurveMotorSetDatabase. This thread generates
30 * one object for each thrust curve motor file and puts it in the second stage.
32 * Stage 2: multiple threads which process individual files. Each process takes
33 * a single motor file and parses out the list of motors it contains.
34 * The list of motors is queued up for the third stage to process.
36 * Stage 3: single thread which processes the list of motors generated in stage 2.
37 * This thread puts all the motors from the list in the motor set database.
39 * It is important that stage 3 be done with a single thread because ThrustCurveMotorSetDatabase
40 * is not thread safe. Even if synchronization were to be done, it is unlikely that parallelizing
41 * this process would improve anything.
45 public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMotorSetDatabase {
47 private static final LogHelper log = Application.getLogger();
48 private final String thrustCurveDirectory;
51 public ConcurrentLoadingThrustCurveMotorSetDatabase(String thrustCurveDirectory) {
52 // configure ThrustCurveMotorSetDatabase as true so we get our own thread in
55 this.thrustCurveDirectory = thrustCurveDirectory;
59 protected void loadMotors() {
61 BookKeeping keeper = new BookKeeping();
65 keeper.waitForFinish();
67 catch ( InterruptedException iex ) {
68 throw new BugException(iex);
75 private void addAll( List<Motor> motors ) {
76 for (Motor m : motors) {
77 addMotor( (ThrustCurveMotor) m);
82 * A class which holds all the threading data.
83 * Implemented as an inner class so we can easily jettison the references when
84 * the processing is terminated.
87 private class BookKeeping {
90 * Executor for Stage 3.
92 private final ExecutorService writerThread;
95 * Executor for Stage 2.
97 private final ExecutorService loaderPool;
100 * Runnable used for Stage 1.
102 private final WorkGenerator workGenerator;
104 private long startTime;
107 * Number of thrust curves loaded
109 private int thrustCurveCount = 0;
112 * Number of files processed.
114 private int fileCount = 0;
117 * We have to hold on to the zip file iterator which is used to load
118 * the system motor files until all processing is done. This is because
119 * closing the iterator prematurely causes all the InputStreams opened
122 private FileIterator iterator;
124 private BookKeeping() {
126 writerThread = Executors.newSingleThreadExecutor( new ThreadFactory() {
128 public Thread newThread(Runnable r) {
129 Thread t = new Thread(r,"MotorWriterThread");
134 loaderPool = Executors.newFixedThreadPool(25, new ThreadFactory() {
137 public Thread newThread(Runnable r) {
138 Thread t = new Thread(r,"MotorLoaderPool-" + threadCount++);
143 workGenerator = new WorkGenerator();
147 private void start() {
149 startTime = System.currentTimeMillis();
151 log.info("Starting motor loading from " + thrustCurveDirectory + " in background thread.");
153 // Run the work generator - in this thread.
158 private void waitForFinish() throws InterruptedException {
160 loaderPool.shutdown();
161 loaderPool.awaitTermination(10, TimeUnit.SECONDS);
162 writerThread.shutdown();
163 writerThread.awaitTermination(10, TimeUnit.SECONDS);
169 long endTime = System.currentTimeMillis();
171 int distinctMotorCount = 0;
172 int distinctThrustCurveCount = 0;
173 distinctMotorCount = motorSets.size();
174 for (ThrustCurveMotorSet set : motorSets) {
175 distinctThrustCurveCount += set.getMotorCount();
178 log.info("Motor loading done, took " + (endTime - startTime) + " ms to load "
179 + fileCount + " files/directories containing "
180 + thrustCurveCount + " thrust curves which contained "
181 + distinctMotorCount + " distinct motors with "
182 + distinctThrustCurveCount + " distinct thrust curves.");
187 private class WorkGenerator implements Runnable {
192 log.info("Loading motors from " + thrustCurveDirectory);
194 iterator = DirectoryIterator.findDirectory(thrustCurveDirectory,
195 new SimpleFileFilter("", false, "eng", "rse"));
197 // Load the packaged thrust curves
198 if (iterator == null) {
199 throw new IllegalStateException("Thrust curve directory " + thrustCurveDirectory +
200 "not found, distribution built wrong");
203 while( iterator.hasNext() ) {
204 Pair<String,InputStream> f = iterator.next();
205 MotorLoader loader = new MotorLoader( f.getV(), f.getU() );
206 loaderPool.execute(loader);
210 // Load the user-defined thrust curves
211 for (File file : ((SwingPreferences) Application.getPreferences()).getUserThrustCurveFiles()) {
212 log.info("Loading motors from " + file);
213 MotorLoader loader = new MotorLoader( file );
214 loaderPool.execute(loader);
220 private class MotorLoader implements Runnable {
222 private final InputStream is;
223 private final String fileName;
225 private final File file;
227 public MotorLoader( File file ) {
231 this.fileName = null;
234 public MotorLoader(InputStream is, String fileName) {
238 this.fileName = fileName;
243 log.debug("Loading motor from " + fileName);
247 if ( file == null ) {
248 motors = MotorLoaderHelper.load(is, fileName);
250 motors = MotorLoaderHelper.load(file);
252 writerThread.submit( new MotorInserter(motors));
258 } catch ( IOException iex ) {
265 private class MotorInserter implements Runnable {
267 private final List<Motor> motors;
269 MotorInserter( List<Motor> motors ) {
270 this.motors = motors;
275 thrustCurveCount += motors.size();
276 ConcurrentLoadingThrustCurveMotorSetDatabase.this.addAll(motors);