1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
package align2;
import dna.ChromosomeArray;
import shared.Shared;
/**
* @author Brian Bushnell
* @date Dec 31, 2012
*
*/
public class ChromLoadThread extends Thread {
public static void main(String[] args){
}
public ChromLoadThread(String fname_, int id_, ChromosomeArray[] r_){
fname=fname_;
id=id_;
array=r_;
}
public static ChromLoadThread load(String fname, int id, ChromosomeArray[] r){
assert(r[id]==null);
ChromLoadThread clt=null;
if(r[id]==null){
increment(1);
clt=new ChromLoadThread(fname, id, r);
clt.start();
}
return clt;
}
public static ChromosomeArray[] loadAll(String pattern, int min, int max, ChromosomeArray[] r){
if(r==null){r=new ChromosomeArray[max+1];}
assert(r.length>=max+1);
int pound=pattern.lastIndexOf('#');
String a=pattern.substring(0, pound);
String b=pattern.substring(pound+1);
ChromLoadThread[] clta=new ChromLoadThread[max];
for(int i=min; i<max; i++){
String fname=(a+i+b);
clta[i]=load(fname, i, r);
}
if(max>=min){ //Load last element in this thread instead of making a new thread.
increment(1);
r[max]=ChromosomeArray.read(a+max+b);
increment(-1);
}
for(int i=min; i<max; i++){
while(r[i]==null){
synchronized(lock){
while(lock[0]>0){
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lock.notify();
}
}
}
}
return r;
}
@Override
public void run(){
try {
array[id]=ChromosomeArray.read(fname);
} catch (Exception e) {
increment(-1);
throw new RuntimeException(e);
}
increment(-1);
}
private static final int increment(int i){
int r;
synchronized(lock){
if(i<=0){
lock[0]+=i;
lock.notify();
}else{
while(lock[0]>=MAX_CONCURRENT){
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
r=lock[0];
}
return r;
}
private final int id;
private final String fname;
private final ChromosomeArray[] array;
public static final int[] lock=new int[1];
public static int MAX_CONCURRENT=Shared.threads();
}
|