Skip to content

Commit 08c55dd

Browse files
committed
fix
1 parent 0588d26 commit 08c55dd

4 files changed

Lines changed: 97 additions & 20 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.nsu.gunko.topic8.task1;
2+
3+
public class CommonRes {
4+
private boolean flag;
5+
6+
public CommonRes() {
7+
flag = true;
8+
}
9+
10+
public boolean getFlag() {
11+
return flag;
12+
}
13+
14+
public void setFlag(boolean flag) {
15+
this.flag = flag;
16+
}
17+
}

‎training/topic8/src/main/java/ru/nsu/gunko/topic8/task1/Consumer.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
public class Consumer implements Runnable {
66
private final BlockingDeque<String> storage;
7+
private final CommonRes commonRes;
78

8-
Consumer(BlockingDeque<String> s) {
9+
public Consumer(BlockingDeque<String> s, CommonRes resC) {
910
storage = s;
11+
commonRes = resC;
1012
}
1113

1214
public void run() {
1315
try {
14-
while (true) {
15-
consume(storage.take());
16+
while (commonRes.getFlag()) {
17+
if (!storage.isEmpty()) {
18+
consume(storage.take());
19+
}
20+
1621
synchronized (Thread.currentThread()) {
1722
try {
1823
Thread.currentThread().wait(75);

‎training/topic8/src/main/java/ru/nsu/gunko/topic8/task1/Main.java‎

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,83 @@ public static void main(String[] args) {
1111

1212
int countP = Integer.parseInt(args[1]);
1313
int countC = Integer.parseInt(args[2]);
14+
int T = Integer.parseInt(args[3]);
1415

15-
Producer p = new Producer(storage);
16-
Consumer c = new Consumer(storage);
16+
ExecutorService serviceP = Executors.newFixedThreadPool(countP);
17+
ExecutorService serviceC = Executors.newFixedThreadPool(countC);
1718

18-
List<Future<?>> futures = new ArrayList<>();
19+
List<Future<?>> futuresP = new ArrayList<>();
20+
List<Future<?>> futuresC = new ArrayList<>();
1921

20-
try (ExecutorService service = Executors.newCachedThreadPool()) {
21-
for (int i = 0; i < countP; ++i) {
22-
futures.add(service.submit(p));
22+
CommonRes resP = new CommonRes();
23+
CommonRes resC = new CommonRes();
24+
25+
for (int i = 0; i < countP; ++i) {
26+
futuresP.add(serviceP.submit(new Producer(storage, resP)));
27+
}
28+
29+
for (int i = 0; i < countC; ++i) {
30+
futuresC.add(serviceC.submit(new Consumer(storage, resC)));
31+
}
32+
33+
synchronized (Thread.currentThread()) {
34+
try {
35+
Thread.currentThread().wait(T * 1000L);
36+
} catch (InterruptedException e) {
37+
throw new RuntimeException(e);
2338
}
39+
}
2440

25-
for (int i = 0; i < countC; ++i) {
26-
futures.add(service.submit(c));
41+
serviceP.shutdown();
42+
resP.setFlag(false);
43+
44+
while (true) {
45+
if (Check.start(futuresP)) {
46+
synchronized (Thread.currentThread()) {
47+
try {
48+
Thread.currentThread().wait(100);
49+
} catch (InterruptedException e) {
50+
throw new RuntimeException(e);
51+
}
52+
}
53+
} else {
54+
System.out.println("Producer's are finished");
55+
break;
2756
}
57+
}
2858

29-
synchronized (Thread.currentThread()) {
30-
Thread.currentThread().wait(1000);
59+
while (true) {
60+
if (!storage.isEmpty()) {
61+
synchronized (Thread.currentThread()) {
62+
try {
63+
Thread.currentThread().wait(100);
64+
} catch (InterruptedException e) {
65+
throw new RuntimeException(e);
66+
}
67+
}
68+
} else {
69+
System.out.println("Storage is empty");
70+
break;
3171
}
72+
}
3273

33-
if (Check.start(futures)) {
34-
service.shutdownNow();
74+
serviceC.shutdown();
75+
resC.setFlag(false);
76+
77+
if (Check.start(futuresC)) {
78+
try {
79+
if (!serviceC.awaitTermination(10, TimeUnit.SECONDS)) {
80+
serviceC.shutdownNow();
81+
if (!serviceC.awaitTermination(10, TimeUnit.SECONDS)) {
82+
System.err.println("Pool did not terminate");
83+
}
84+
}
85+
} catch (InterruptedException ex) {
86+
serviceC.shutdownNow();
87+
Thread.currentThread().interrupt();
3588
}
36-
} catch (InterruptedException ex) {
37-
System.err.println(ex.getLocalizedMessage());
3889
}
90+
91+
System.out.println("Consumer's are finished");
3992
}
40-
}
93+
}

‎training/topic8/src/main/java/ru/nsu/gunko/topic8/task1/Producer.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
public class Producer implements Runnable {
66
private final BlockingDeque<String> storage;
77
private int count;
8+
private final CommonRes commonRes;
89

9-
Producer(BlockingDeque<String> s) {
10+
Producer(BlockingDeque<String> s, CommonRes resP) {
1011
storage = s;
1112
count = 1;
13+
commonRes = resP;
1214
}
1315

1416
public void run() {
1517
try {
16-
while (true) {
18+
while (commonRes.getFlag()) {
1719
storage.put(produce());
1820
++count;
1921
}

0 commit comments

Comments
 (0)