| 1 |
package ukmer; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.concurrent.atomic.AtomicInteger; |
| 5 |
|
| 6 |
import shared.KillSwitch; |
| 7 |
import shared.Shared; |
| 8 |
import shared.Tools; |
| 9 |
|
| 10 |
public class OwnershipThread extends Thread { |
| 11 |
|
| 12 |
public static void clear(AbstractKmerTableU[] tables){ |
| 13 |
process(tables, CLEAR); |
| 14 |
} |
| 15 |
|
| 16 |
public static void initialize(AbstractKmerTableU[] tables){ |
| 17 |
process(tables, INITIALIZE); |
| 18 |
} |
| 19 |
|
| 20 |
private static void process(AbstractKmerTableU[] tables, int mode){ |
| 21 |
if(tables.length<2){ |
| 22 |
if(mode==INITIALIZE){ |
| 23 |
for(AbstractKmerTableU akt : tables){akt.initializeOwnership();} |
| 24 |
}else if(mode==CLEAR){ |
| 25 |
for(AbstractKmerTableU akt : tables){akt.clearOwnership();} |
| 26 |
}else{ |
| 27 |
KillSwitch.kill("Bad mode: "+mode); |
| 28 |
} |
| 29 |
return; |
| 30 |
} |
| 31 |
final int threads=Tools.min(Shared.threads(), tables.length); |
| 32 |
final AtomicInteger next=new AtomicInteger(0); |
| 33 |
ArrayList<OwnershipThread> alpt=new ArrayList<OwnershipThread>(threads); |
| 34 |
for(int i=0; i<threads; i++){alpt.add(new OwnershipThread(tables, mode, next));} |
| 35 |
for(OwnershipThread pt : alpt){pt.start();} |
| 36 |
|
| 37 |
for(OwnershipThread pt : alpt){ |
| 38 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 39 |
try { |
| 40 |
pt.join(); |
| 41 |
} catch (InterruptedException e) { |
| 42 |
|
| 43 |
e.printStackTrace(); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public OwnershipThread(AbstractKmerTableU[] tables_, int mode_, AtomicInteger next_){ |
| 50 |
tables=tables_; |
| 51 |
mode=mode_; |
| 52 |
next=next_; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void run(){ |
| 57 |
for(int i=next.getAndIncrement(); i<tables.length; i=next.getAndIncrement()){ |
| 58 |
if(mode==INITIALIZE){ |
| 59 |
tables[i].initializeOwnership(); |
| 60 |
}else if(mode==CLEAR){ |
| 61 |
tables[i].clearOwnership(); |
| 62 |
}else{ |
| 63 |
KillSwitch.kill("Bad mode: "+mode); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
private final AbstractKmerTableU[] tables; |
| 69 |
private final AtomicInteger next; |
| 70 |
private final int mode; |
| 71 |
|
| 72 |
public static final int INITIALIZE=0; |
| 73 |
public static final int CLEAR=1; |
| 74 |
|
| 75 |
} |