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.LinkedBlockingQueue;
10 import java.util.concurrent.ThreadFactory;
11 import java.util.concurrent.ThreadPoolExecutor;
12 import java.util.concurrent.TimeUnit;
14 import net.sf.openrocket.database.ThrustCurveMotorSet;
15 import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
16 import net.sf.openrocket.file.iterator.DirectoryIterator;
17 import net.sf.openrocket.file.iterator.FileIterator;
18 import net.sf.openrocket.file.motor.MotorLoaderHelper;
19 import net.sf.openrocket.gui.util.SimpleFileFilter;
20 import net.sf.openrocket.gui.util.SwingPreferences;
21 import net.sf.openrocket.logging.LogHelper;
22 import net.sf.openrocket.motor.Motor;
23 import net.sf.openrocket.motor.ThrustCurveMotor;
24 import net.sf.openrocket.util.BugException;
25 import net.sf.openrocket.util.Pair;
28 * Load motors in parallel using a three stage pipeline.
30 * Stage 1: single thread managed by the ThrustCurveMotorSetDatabase. This thread generates
31 * one object for each thrust curve motor file and puts it in the second stage.
33 * Stage 2: multiple threads which process individual files. Each process takes
34 * a single motor file and parses out the list of motors it contains.
35 * The list of motors is queued up for the third stage to process.
37 * Stage 3: single thread which processes the list of motors generated in stage 2.
38 * This thread puts all the motors from the list in the motor set database.
40 * It is important that stage 3 be done with a single thread because ThrustCurveMotorSetDatabase
41 * is not thread safe. Even if synchronization were to be done, it is unlikely that parallelizing
42 * this process would improve anything.
46 public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMotorSetDatabase {
48 private static final LogHelper log = Application.getLogger();
49 private final String thrustCurveDirectory;
52 public ConcurrentLoadingThrustCurveMotorSetDatabase(String thrustCurveDirectory) {
53 // configure ThrustCurveMotorSetDatabase as true so we get our own thread in
56 this.thrustCurveDirectory = thrustCurveDirectory;
60 protected void loadMotors() {
62 BookKeeping keeper = new BookKeeping();
66 keeper.waitForFinish();
68 catch ( InterruptedException iex ) {
69 throw new BugException(iex);
76 private void addAll( List<Motor> motors ) {
77 for (Motor m : motors) {
78 addMotor( (ThrustCurveMotor) m);
83 * A class which holds all the threading data.
84 * Implemented as an inner class so we can easily jettison the references when
85 * the processing is terminated.
88 private class BookKeeping {
91 * Executor for Stage 3.
93 private final ExecutorService writerThread;
96 * Executor for Stage 2.
98 private final ExecutorService loaderPool;
101 * Runnable used for Stage 1.
103 private final WorkGenerator workGenerator;
105 private long startTime;
108 * Number of thrust curves loaded
110 private int thrustCurveCount = 0;
113 * Number of files processed.
115 private int fileCount = 0;
118 * We have to hold on to the zip file iterator which is used to load
119 * the system motor files until all processing is done. This is because
120 * closing the iterator prematurely causes all the InputStreams opened
123 private FileIterator iterator;
125 private BookKeeping() {
127 writerThread = Executors.newSingleThreadExecutor( new ThreadFactory() {
129 public Thread newThread(Runnable r) {
130 Thread t = new Thread(r,"MotorWriterThread");
135 loaderPool = new ThreadPoolExecutor(25,25, 2, TimeUnit.SECONDS,
136 new LinkedBlockingQueue<Runnable>(),
137 new ThreadFactory() {
140 public Thread newThread(Runnable r) {
141 Thread t = new Thread(r,"MotorLoaderPool-" + threadCount++);
146 workGenerator = new WorkGenerator();
150 private void start() {
152 startTime = System.currentTimeMillis();
154 log.info("Starting motor loading from " + thrustCurveDirectory + " in background thread.");
156 // Run the work generator - in this thread.
161 private void waitForFinish() throws InterruptedException {
163 loaderPool.shutdown();
164 loaderPool.awaitTermination(10, TimeUnit.SECONDS);
165 writerThread.shutdown();
166 writerThread.awaitTermination(10, TimeUnit.SECONDS);
172 long endTime = System.currentTimeMillis();
174 int distinctMotorCount = 0;
175 int distinctThrustCurveCount = 0;
176 distinctMotorCount = motorSets.size();
177 for (ThrustCurveMotorSet set : motorSets) {
178 distinctThrustCurveCount += set.getMotorCount();
181 log.info("Motor loading done, took " + (endTime - startTime) + " ms to load "
182 + fileCount + " files/directories containing "
183 + thrustCurveCount + " thrust curves which contained "
184 + distinctMotorCount + " distinct motors with "
185 + distinctThrustCurveCount + " distinct thrust curves.");
190 private class WorkGenerator implements Runnable {
195 log.info("Loading motors from " + thrustCurveDirectory);
197 iterator = DirectoryIterator.findDirectory(thrustCurveDirectory,
198 new SimpleFileFilter("", false, "eng", "rse"));
200 // Load the packaged thrust curves
201 if (iterator == null) {
202 throw new IllegalStateException("Thrust curve directory " + thrustCurveDirectory +
203 "not found, distribution built wrong");
206 while( iterator.hasNext() ) {
207 Pair<String,InputStream> f = iterator.next();
208 MotorLoader loader = new MotorLoader( f.getV(), f.getU() );
209 loaderPool.execute(loader);
213 // Load the user-defined thrust curves
214 for (File file : ((SwingPreferences) Application.getPreferences()).getUserThrustCurveFiles()) {
215 log.info("Loading motors from " + file);
216 MotorLoader loader = new MotorLoader( file );
217 loaderPool.execute(loader);
223 private class MotorLoader implements Runnable {
225 private final InputStream is;
226 private final String fileName;
228 private final File file;
230 public MotorLoader( File file ) {
234 this.fileName = null;
237 public MotorLoader(InputStream is, String fileName) {
241 this.fileName = fileName;
246 log.debug("Loading motor from " + fileName);
250 if ( file == null ) {
251 motors = MotorLoaderHelper.load(is, fileName);
253 motors = MotorLoaderHelper.load(file);
255 writerThread.submit( new MotorInserter(motors));
261 } catch ( IOException iex ) {
268 private class MotorInserter implements Runnable {
270 private final List<Motor> motors;
272 MotorInserter( List<Motor> motors ) {
273 this.motors = motors;
278 thrustCurveCount += motors.size();
279 ConcurrentLoadingThrustCurveMotorSetDatabase.this.addAll(motors);