-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubGridThread.java
44 lines (39 loc) · 1.28 KB
/
SubGridThread.java
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
import java.util.HashMap;
import java.util.concurrent.Semaphore;
/**
* sub-grid 1: 0,0 sub-grid 2: 0,3 sub-grid 3: 0,6 sub-grid 4: 3,0 sub-grid 5:
* 3,3 sub-grid 6: 3,6 sub-grid 7: 6,0 sub-grid 8: 6,3 sub-grid 9: 6,6
*/
public class SubGridThread extends Thread {
private int[][] grid;
private Validator validator;
private Semaphore sem;
private final HashMap<Integer, String> subGridIndex = new HashMap<Integer, String>() {
{
put(0, "123");
put(3, "456");
put(6, "789");
}
};
final private String template = "Thread 3, SubGrid R%1$s-C%2$s, %3$s";
public SubGridThread(int[][] grid, Semaphore sem) {
this.sem = sem;
this.grid = grid;
validator = new Validator(grid);
}
@Override
public void run() {
try {
sem.acquire();
for (int i = 0; i < 9; i++) {
String row = subGridIndex.get(validator.subGrids[i][0]);
String col = subGridIndex.get(validator.subGrids[i][1]);
System.out.println(String.format(template, row, col, validator.isValidSubGrid(i)));
}
} catch (InterruptedException err) {
System.out.println(err);
} finally {
sem.release();
}
}
}