-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlave2.java
More file actions
66 lines (54 loc) · 2 KB
/
Slave2.java
File metadata and controls
66 lines (54 loc) · 2 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
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Slave2 extends Thread {
private BlockingQueue<Packet> queue;
private ClientHandler clientHandler;
public Slave2() {
}
public void setQ(BlockingQueue<Packet> blockingQueue) {
this.queue = blockingQueue;
}
// Setter for ClientHandler
public void setClientHandler(ClientHandler clientHandler) {
this.clientHandler = clientHandler;
}
public String job1() throws InterruptedException {
Thread.sleep(3000); // Simulate work (different timing for Slave2)
return "Executed operation 1 in slave 2";
}
public String job2() throws InterruptedException {
Thread.sleep(8000); // Simulate work (different timing for Slave2)
return "Executed operation 2 in slave 2";
}
@Override
public void run() {
while (true) { // Keep processing jobs from the queue
try {
// Take a job from the queue (blocks if the queue is empty)
Packet p = queue.take();
Operation o = p.getOperation();
String result;
// Process the job based on its type
switch (o) {
case JOB_1:
result = job1();
break;
case JOB_2:
result = job2();
break;
default:
result = "Unknown job type.";
}
clientHandler.updateCounterAfterCompletion(o, 2); // Notify master to update counter
System.out.println("Slave 2 completed: " + result);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Slave 2 interrupted: " + e.getMessage());
break; // Exit loop on interruption
}
}
}
public BlockingQueue<Packet> getQueue() {
return queue;
}
}