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