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