-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskControlThread.java
More file actions
128 lines (124 loc) · 3.69 KB
/
TaskControlThread.java
File metadata and controls
128 lines (124 loc) · 3.69 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package map_reduce_demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Queue;
public class TaskControlThread extends Thread{
Queue<Job> jobqueue;
ArrayList<TaskClientState> tcsarray;
WorkState ws;
WorkerCount wc;
public TaskControlThread(Queue<Job> jq,ArrayList<TaskClientState> tcsa,WorkState w,WorkerCount Wc){
jobqueue = jq;
tcsarray = tcsa;
ws = w;
wc = Wc;
}
public void run(){
synchronized(ws){
while(true){
while(ws.IsLeisure() || ws.IsMBusy() || ws.IsRBusy()){
try {
ws.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(ws.IsMReady()){
wc.set(tcsarray.size(),tcsarray.size()/*jobqueue.peek().GetMT(),jobqueue.peek().GetMT()*/);
FileOperations.separateFile(jobqueue.peek().getDataName(),tcsarray.size() );
synchronized(tcsarray){
while(tcsarray.isEmpty()){
try {
tcsarray.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(TaskClientState tcs : tcsarray){
Start st =new Start(tcs,jobqueue.peek().getFileName());
st.start();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
int i = 0;
for(int count = wc.getMT();count > 0;){
for(TaskClientState tcs : tcsarray){
count--;
String file = jobqueue.peek().getDataName();
int index = file.lastIndexOf(".");
String fname = file.substring(0, index);
String extend = file.substring(index+1);
AddaMapper adm = new AddaMapper(tcs,fname + i + "."+extend);
i = i + 1;
adm.start();
if(count <= 0){
break;
}
}
}
}
ws.SetasMBusy();
}
else if(ws.IsRReady()){
String [] fpath = new String[tcsarray.size()];
for(int i = 0; i<tcsarray.size();i++)
fpath[i] = "bible"+ i+".mpd";
FileOperations.mergeFiles(fpath,"bible_merge.txt");
System.out.println("Merged!");
File f = new File(jobqueue.peek().getFileName());
FileInputStream fis;
try {
fis = new FileInputStream(f);
FileClassLoader cl =new FileClassLoader(fis);
Class c = cl.loadClass("mapreduce.test.MyMapp");
Reducer m = new Reducer();
m.serverCombine("bible_merge.txt", c);
fis.close();
System.out.println("Server Combining has been done!\n Reducing...");
FileOperations.separateFile("bible_merge.scb",tcsarray.size());
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
int i = 0;
for(int count = wc.getRT();count > 0;){
for(TaskClientState tcs : tcsarray){
count--;
AddaReducer adr = new AddaReducer(tcs,"bible_merge" + i + ".scb");
i++;
adr.start();
if(count <= 0){
break;
}
}
}
ws.SetasMBusy();
}
else if(ws.IsFinished()){
String [] fpath = new String[tcsarray.size()];
for(int i = 0; i<tcsarray.size();i++)
fpath[i] = "bible"+ i+".rdc";
FileOperations.mergeFiles(fpath,"bible_merge.rdc");
System.out.println("Merged!");
jobqueue.poll();
if(jobqueue.isEmpty())
ws.SetasLeisure();
else
ws.SetasMReady();
}
}
}
}
}