~ubuntu-branches/ubuntu/saucy/fastqc/saucy-proposed

« back to all changes in this revision

Viewing changes to uk/ac/babraham/FastQC/Analysis/AnalysisQueue.java

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2012-11-20 13:38:32 UTC
  • Revision ID: package-import@ubuntu.com-20121120133832-psohzlsak64g7bdy
Tags: upstream-0.10.1+dfsg
ImportĀ upstreamĀ versionĀ 0.10.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright Copyright 2010-12 Simon Andrews
 
3
 *
 
4
 *    This file is part of FastQC.
 
5
 *
 
6
 *    FastQC is free software; you can redistribute it and/or modify
 
7
 *    it under the terms of the GNU General Public License as published by
 
8
 *    the Free Software Foundation; either version 3 of the License, or
 
9
 *    (at your option) any later version.
 
10
 *
 
11
 *    FastQC is distributed in the hope that it will be useful,
 
12
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *    GNU General Public License for more details.
 
15
 *
 
16
 *    You should have received a copy of the GNU General Public License
 
17
 *    along with FastQC; if not, write to the Free Software
 
18
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
package uk.ac.babraham.FastQC.Analysis;
 
21
 
 
22
import java.util.LinkedList;
 
23
 
 
24
import uk.ac.babraham.FastQC.Modules.QCModule;
 
25
import uk.ac.babraham.FastQC.Sequence.SequenceFile;
 
26
 
 
27
public class AnalysisQueue implements Runnable, AnalysisListener{
 
28
 
 
29
        private static AnalysisQueue instance = new AnalysisQueue();
 
30
        
 
31
        private LinkedList<AnalysisRunner>queue = new LinkedList<AnalysisRunner>();
 
32
        
 
33
        private int availableSlots = 1;
 
34
        private int usedSlots = 0;
 
35
        
 
36
        public static AnalysisQueue getInstance () {
 
37
                return instance;
 
38
        }
 
39
        
 
40
        private AnalysisQueue () {
 
41
                
 
42
                if (System.getProperty("fastqc.threads") != null) {
 
43
                        try {
 
44
                                availableSlots = Integer.parseInt(System.getProperty("fastqc.threads"));
 
45
                        }
 
46
                        catch (NumberFormatException nfe) {
 
47
                                System.err.println("Thead limit '"+System.getProperty("fastqc.threads")+" wasn't a number");
 
48
                                availableSlots = 1;
 
49
                        }
 
50
                        
 
51
                        if (availableSlots < 1) {
 
52
                                System.err.println("Thead limit '"+System.getProperty("fastqc.threads")+" must be > 1");
 
53
                                availableSlots = 1;
 
54
                        }
 
55
                        
 
56
                }
 
57
                
 
58
                Thread t = new Thread(this);
 
59
                t.start();
 
60
        }
 
61
        
 
62
        public void addToQueue (AnalysisRunner runner) {
 
63
                queue.add(runner);
 
64
        }
 
65
 
 
66
        public void run() {
 
67
 
 
68
                while (true) {
 
69
//                      System.err.println("Status available="+availableSlots+" used="+usedSlots+" queue="+queue.size());
 
70
                        if (availableSlots > usedSlots && queue.size() > 0) {
 
71
                                ++usedSlots;
 
72
                                AnalysisRunner currentRun = queue.getFirst();
 
73
                                queue.removeFirst();
 
74
                                currentRun.addAnalysisListener(this);
 
75
                                Thread t = new Thread(currentRun);
 
76
                                t.start();
 
77
                        }
 
78
                        
 
79
                        try {
 
80
                                Thread.sleep(500);
 
81
                        } catch (InterruptedException e) {}
 
82
                }
 
83
        }
 
84
 
 
85
        public void analysisComplete(SequenceFile file, QCModule[] results) {
 
86
                --usedSlots;
 
87
        }
 
88
 
 
89
        public void analysisUpdated(SequenceFile file, int sequencesProcessed, int percentComplete) {}
 
90
 
 
91
        public void analysisExceptionReceived(SequenceFile file, Exception e) {
 
92
                --usedSlots;
 
93
        }
 
94
 
 
95
        public void analysisStarted(SequenceFile file) {}
 
96
        
 
97
        
 
98
}