Skip to content

Commit 0f64c11

Browse files
committed
init
1 parent e2ec6fd commit 0f64c11

6 files changed

Lines changed: 173 additions & 0 deletions

File tree

‎training/topic8/.gitignore‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

‎training/topic8/pom.xml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.nsu.gunko.topic8.task1</groupId>
8+
<artifactId>topic8</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.nsu.gunko.topic8.task1;
2+
3+
import java.util.List;
4+
import java.util.concurrent.Future;
5+
6+
public class Check {
7+
public static boolean start(List<Future<?>> futures) {
8+
for (Future<?> future : futures) {
9+
if (!future.isDone()) {
10+
return true;
11+
}
12+
}
13+
return false;
14+
}
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ru.nsu.gunko.topic8.task1;
2+
3+
import java.util.concurrent.BlockingDeque;
4+
5+
public class Consumer implements Runnable {
6+
private final BlockingDeque<String> storage;
7+
8+
Consumer(BlockingDeque<String> s) {
9+
storage = s;
10+
}
11+
12+
public void run() {
13+
try {
14+
while (true) {
15+
consume(storage.take());
16+
synchronized (Thread.currentThread()) {
17+
try {
18+
Thread.currentThread().wait(75);
19+
} catch (InterruptedException ex) {
20+
System.err.println(ex.getLocalizedMessage());
21+
}
22+
}
23+
}
24+
} catch (InterruptedException ex) {
25+
System.err.println(ex.getLocalizedMessage());
26+
}
27+
}
28+
29+
void consume(Object x) {
30+
String name = Thread.currentThread().getName();
31+
System.out.println("c" + name.substring(name.length()-1) + " consumes " + x);
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.nsu.gunko.topic8.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.*;
6+
7+
public class Main {
8+
public static void main(String[] args) {
9+
int count = Integer.parseInt(args[0]);
10+
BlockingDeque<String> storage = new LinkedBlockingDeque<>(count);
11+
12+
int countP = Integer.parseInt(args[1]);
13+
int countC = Integer.parseInt(args[2]);
14+
15+
Producer p = new Producer(storage);
16+
Consumer c = new Consumer(storage);
17+
18+
List<Future<?>> futures = new ArrayList<>();
19+
20+
try (ExecutorService service = Executors.newCachedThreadPool()) {
21+
for (int i = 0; i < countP; ++i) {
22+
futures.add(service.submit(p));
23+
}
24+
25+
for (int i = 0; i < countC; ++i) {
26+
futures.add(service.submit(c));
27+
}
28+
29+
synchronized (Thread.currentThread()) {
30+
Thread.currentThread().wait(1000);
31+
}
32+
33+
if (Check.start(futures)) {
34+
service.shutdownNow();
35+
}
36+
} catch (InterruptedException ex) {
37+
System.err.println(ex.getLocalizedMessage());
38+
}
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ru.nsu.gunko.topic8.task1;
2+
3+
import java.util.concurrent.BlockingDeque;
4+
5+
public class Producer implements Runnable {
6+
private final BlockingDeque<String> storage;
7+
private int count;
8+
9+
Producer(BlockingDeque<String> s) {
10+
storage = s;
11+
count = 1;
12+
}
13+
14+
public void run() {
15+
try {
16+
while (true) {
17+
storage.put(produce());
18+
++count;
19+
}
20+
} catch (InterruptedException ex) {
21+
System.err.println(ex.getLocalizedMessage());
22+
}
23+
}
24+
25+
String produce() {
26+
String name = Thread.currentThread().getName();
27+
return "p" + name.substring(name.length()-1) + "-" + count;
28+
}
29+
}
30+

0 commit comments

Comments
 (0)