File: OwnershipThread.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (75 lines) | stat: -rwxr-xr-x 1,980 bytes parent folder | download | duplicates (4)
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 kmer;

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(AbstractKmerTable[] tables){
		process(tables, CLEAR);
	}
	
	public static void initialize(AbstractKmerTable[] tables){
		process(tables, INITIALIZE);
	}
	
	private static void process(AbstractKmerTable[] tables, int mode){
		if(tables.length<2){
			if(mode==INITIALIZE){
				for(AbstractKmerTable akt : tables){akt.initializeOwnership();}
			}else if(mode==CLEAR){
				for(AbstractKmerTable 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(AbstractKmerTable[] 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 AbstractKmerTable[] tables;
	private final AtomicInteger next;
	private final int mode;

	public static final int INITIALIZE=0;
	public static final int CLEAR=1;
	
}