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
|
package ukmer;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import shared.KillSwitch;
import shared.Shared;
import shared.Tools;
public class OwnershipThread extends Thread {
public static void clear(AbstractKmerTableU[] tables){
process(tables, CLEAR);
}
public static void initialize(AbstractKmerTableU[] tables){
process(tables, INITIALIZE);
}
private static void process(AbstractKmerTableU[] tables, int mode){
if(tables.length<2){
if(mode==INITIALIZE){
for(AbstractKmerTableU akt : tables){akt.initializeOwnership();}
}else if(mode==CLEAR){
for(AbstractKmerTableU akt : tables){akt.clearOwnership();}
}else{
KillSwitch.kill("Bad mode: "+mode);
}
return;
}
final int threads=Tools.min(Shared.threads(), tables.length);
final AtomicInteger next=new AtomicInteger(0);
ArrayList<OwnershipThread> alpt=new ArrayList<OwnershipThread>(threads);
for(int i=0; i<threads; i++){alpt.add(new OwnershipThread(tables, mode, next));}
for(OwnershipThread pt : alpt){pt.start();}
for(OwnershipThread pt : alpt){
while(pt.getState()!=Thread.State.TERMINATED){
try {
pt.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public OwnershipThread(AbstractKmerTableU[] tables_, int mode_, AtomicInteger next_){
tables=tables_;
mode=mode_;
next=next_;
}
@Override
public void run(){
for(int i=next.getAndIncrement(); i<tables.length; i=next.getAndIncrement()){
if(mode==INITIALIZE){
tables[i].initializeOwnership();
}else if(mode==CLEAR){
tables[i].clearOwnership();
}else{
KillSwitch.kill("Bad mode: "+mode);
}
}
}
private final AbstractKmerTableU[] tables;
private final AtomicInteger next;
private final int mode;
public static final int INITIALIZE=0;
public static final int CLEAR=1;
}
|