| 1 |
package ukmer; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.concurrent.atomic.AtomicInteger; |
| 5 |
import java.util.concurrent.atomic.AtomicLong; |
| 6 |
|
| 7 |
import fileIO.ByteStreamWriter; |
| 8 |
import kmer.DumpThread; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Tools; |
| 11 |
import structures.ByteBuilder; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class DumpThreadU extends Thread{ |
| 19 |
|
| 20 |
public static boolean dump(final int k, final int mincount, final int maxcount, final AbstractKmerTableU[] tables, final ByteStreamWriter bsw, AtomicLong remaining){ |
| 21 |
final int threads=DumpThread.NUM_THREADS>0 ? DumpThread.NUM_THREADS : Tools.min(tables.length, (Tools.mid(1, Shared.threads()-1, 6))); |
| 22 |
final AtomicInteger lock=new AtomicInteger(0); |
| 23 |
final ArrayList<DumpThreadU> list=new ArrayList<DumpThreadU>(threads); |
| 24 |
for(int i=0; i<threads; i++){ |
| 25 |
list.add(new DumpThreadU(k, mincount, maxcount, lock, tables, bsw, remaining)); |
| 26 |
} |
| 27 |
for(DumpThreadU t : list){t.start();} |
| 28 |
boolean success=true; |
| 29 |
for(DumpThreadU t : list){ |
| 30 |
while(t.getState()!=Thread.State.TERMINATED){ |
| 31 |
try { |
| 32 |
t.join(); |
| 33 |
} catch (InterruptedException e) { |
| 34 |
|
| 35 |
e.printStackTrace(); |
| 36 |
} |
| 37 |
} |
| 38 |
success&=t.success; |
| 39 |
} |
| 40 |
return success; |
| 41 |
} |
| 42 |
|
| 43 |
public DumpThreadU(final int k_, final int mincount_, final int maxcount_, final AtomicInteger nextTable_, final AbstractKmerTableU[] tables_, final ByteStreamWriter bsw_, AtomicLong remaining_){ |
| 44 |
k=k_; |
| 45 |
mincount=mincount_; |
| 46 |
maxcount=maxcount_; |
| 47 |
nextTable=nextTable_; |
| 48 |
tables=tables_; |
| 49 |
bsw=bsw_; |
| 50 |
remaining=remaining_; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void run(){ |
| 55 |
final ByteBuilder bb=new ByteBuilder(16300); |
| 56 |
for(int i=nextTable.getAndIncrement(); i<tables.length; i=nextTable.getAndIncrement()){ |
| 57 |
AbstractKmerTableU t=tables[i]; |
| 58 |
t.dumpKmersAsBytes_MT(bsw, bb, k, mincount, maxcount, remaining); |
| 59 |
} |
| 60 |
if(bb.length()>0){ |
| 61 |
synchronized(bsw){bsw.addJob(bb);} |
| 62 |
} |
| 63 |
success=true; |
| 64 |
} |
| 65 |
|
| 66 |
final int k; |
| 67 |
final int mincount; |
| 68 |
final int maxcount; |
| 69 |
final AtomicInteger nextTable; |
| 70 |
final AtomicLong remaining; |
| 71 |
final AbstractKmerTableU[] tables; |
| 72 |
final ByteStreamWriter bsw; |
| 73 |
boolean success=false; |
| 74 |
|
| 75 |
} |