Skip to content

Commit 797b73f

Browse files
committed
Java : Task 3 Completely concurrent. Added countdown latch
1 parent 38dd09e commit 797b73f

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

JavaProgramming/src/task3/teastall/Constants.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package task3.teastall;
22

3+
import java.time.format.DateTimeFormatter;
34
import java.util.HashMap;
45
import java.util.Map;
56

@@ -9,6 +10,8 @@ public class Constants {
910
public static final int SERVER_PORT = 5000;
1011
public static final String GET_AVAILABLE_LIST = "GET_AVAILABLE_LIST";
1112
public static final String PLACE_ORDER = "PLACE_ORDER";
13+
public static final int ORDERS_THRESHHOLD = 10;
14+
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm 'on'9 cccc dd,LLLL ");
1215

1316
public static Map<String, Integer> getInitialItems() {
1417
Map<String, Integer> items = new HashMap<>();

JavaProgramming/src/task3/teastall/server/ItemProcessor.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package task3.teastall.server;
22

3+
import java.util.concurrent.CountDownLatch;
4+
35
public class ItemProcessor extends Thread {
46
private String item;
57
private int quantity;
68
private Server server;
79
private int delay;
8-
9-
ItemProcessor(Server server, String item, int quantity) {
10+
private CountDownLatch latch;
11+
ItemProcessor(Server server, String item, int quantity, CountDownLatch latch) {
1012
super();
1113
this.item = item;
1214
this.server = server;
1315
this.quantity = quantity;
16+
this.latch = latch;
1417
}
1518

1619
@Override
@@ -21,6 +24,7 @@ public void run() {
2124
delay = this.server.getItemDelay().getOrDefault(item, -1);
2225
delay = delay * quantity + 2;
2326
}
27+
latch.countDown();
2428
}
2529

2630
int getDelay() {

JavaProgramming/src/task3/teastall/server/OrderProcessor.java

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package task3.teastall.server;
22

3+
import task3.teastall.Constants;
4+
35
import java.io.DataOutputStream;
46
import java.io.IOException;
57
import java.net.Socket;
68
import java.time.LocalDateTime;
79
import java.util.Map;
10+
import java.util.concurrent.CountDownLatch;
811

912
public class OrderProcessor extends Thread {
1013
private Map<String, Integer> order;
@@ -28,22 +31,25 @@ public void run() {
2831
// Create thread for all the items and join on them
2932
ItemProcessor[] itemProcessors = new ItemProcessor[order.size()];
3033
int count = 0;
34+
CountDownLatch latch = new CountDownLatch(order.size());
35+
3136
for (Map.Entry<String, Integer> entry : order.entrySet()) {
32-
itemProcessors[count] = new ItemProcessor(server, entry.getKey(), entry.getValue());
37+
itemProcessors[count] = new ItemProcessor(server, entry.getKey(), entry.getValue(), latch);
3338
itemProcessors[count].start();
3439
count++;
3540
}
36-
for (ItemProcessor itemProcessor : itemProcessors) {
37-
try {
38-
itemProcessor.join();
39-
int d = itemProcessor.getDelay();
4041

41-
// check if the order can be fulfilled or not
42-
if (d < 0) available = false;
43-
if (delay < d) delay = d;
44-
} catch (InterruptedException e) {
45-
e.printStackTrace();
46-
}
42+
try {
43+
latch.await();
44+
} catch (InterruptedException e) {
45+
e.printStackTrace();
46+
}
47+
48+
for (ItemProcessor itemProcessor : itemProcessors) {
49+
int d = itemProcessor.getDelay();
50+
// check if the order can be fulfilled or not
51+
if (d < 0) available = false;
52+
if (delay < d) delay = d;
4753
}
4854
}
4955

@@ -69,7 +75,7 @@ LocalDateTime sendResult(LocalDateTime timeStamp) {
6975
timeStamp = timeStamp.plusMinutes(getDelay());
7076
try {
7177
if (dataOutputStream != null) {
72-
dataOutputStream.writeUTF("The order will be delivered at " + timeStamp.toString() + ".");
78+
dataOutputStream.writeUTF("The order will be delivered by " + timeStamp.format(Constants.DATE_TIME_FORMATTER));
7379
}
7480
} catch (IOException e) {
7581
e.printStackTrace();

JavaProgramming/src/task3/teastall/server/OrdersProcessor.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package task3.teastall.server;
22

3+
import task3.teastall.Constants;
4+
35
import java.time.LocalDateTime;
46
import java.util.ArrayList;
57
import java.util.List;
@@ -20,12 +22,13 @@ public class OrdersProcessor extends Thread {
2022
public void run() {
2123
while (true) {
2224
if (orders.size() > 0) {
23-
while (orders.size() > 0) {
25+
int count = 0;
26+
while (orders.size() > 0 && count < Constants.ORDERS_THRESHHOLD) {
2427
OrderProcessor order = orders.get(0);
2528
orders.remove(0);
2629
processingOrders.add(order);
2730
order.start();
28-
31+
count++;
2932
}
3033
while (processingOrders.size() > 0) {
3134
OrderProcessor order = processingOrders.get(0);

0 commit comments

Comments
 (0)