Skip to content

Commit e2ec6fd

Browse files
committed
init task1
1 parent bac730b commit e2ec6fd

6 files changed

Lines changed: 169 additions & 0 deletions

File tree

‎training/topic7/.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/topic7/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.topic7.task1</groupId>
8+
<artifactId>topic7</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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ru.nsu.gunko.topic7.task1;
2+
3+
public class Consumer implements Runnable {
4+
private final Storage<String> storage;
5+
6+
Consumer(Storage<String> s) {
7+
storage = s;
8+
}
9+
10+
public void run() {
11+
try {
12+
while (true) {
13+
consume(storage.take());
14+
synchronized (Thread.currentThread()) {
15+
try {
16+
Thread.currentThread().wait(75);
17+
} catch (InterruptedException ex) {
18+
System.err.println(ex.getLocalizedMessage());
19+
}
20+
}
21+
}
22+
} catch (InterruptedException ex) {
23+
System.err.println(ex.getLocalizedMessage());
24+
}
25+
}
26+
27+
void consume(Object x) {
28+
String name = Thread.currentThread().getName();
29+
System.out.println("c" + name.substring(name.length()-1) + " consumes " + x);
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.nsu.gunko.topic7.task1;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Storage<String> storage = new Storage<>(Integer.parseInt(args[0]));
6+
7+
int countP = Integer.parseInt(args[1]);
8+
int countC = Integer.parseInt(args[2]);
9+
10+
Producer p = new Producer(storage);
11+
Consumer c = new Consumer(storage);
12+
13+
for (int i = 0; i < countP; ++i) {
14+
new Thread(p).start();
15+
}
16+
17+
for (int i = 0; i < countC; ++i) {
18+
new Thread(c).start();
19+
}
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.nsu.gunko.topic7.task1;
2+
3+
public class Producer implements Runnable {
4+
private final Storage<String> storage;
5+
private int count;
6+
7+
Producer(Storage<String> s) {
8+
storage = s;
9+
count = 1;
10+
}
11+
12+
public void run() {
13+
try {
14+
while (true) {
15+
storage.put(produce());
16+
++count;
17+
}
18+
} catch (InterruptedException ex) {
19+
System.err.println(ex.getLocalizedMessage());
20+
}
21+
}
22+
23+
String produce() {
24+
String name = Thread.currentThread().getName();
25+
return "p" + name.substring(name.length()-1) + "-" + count;
26+
}
27+
}
28+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ru.nsu.gunko.topic7.task1;
2+
3+
import java.util.*;
4+
5+
public class Storage<T> {
6+
List<T> box;
7+
int count;
8+
9+
Storage(int count) {
10+
this.count = count;
11+
box = new ArrayList<>();
12+
}
13+
14+
synchronized public void put(T obj) throws InterruptedException {
15+
while (box.size() == count) {
16+
wait();
17+
}
18+
19+
box.add(obj);
20+
notifyAll();
21+
}
22+
23+
synchronized public T take() throws InterruptedException {
24+
while (box.isEmpty()) {
25+
wait();
26+
}
27+
28+
T obj = box.getLast();
29+
box.removeLast();
30+
notifyAll();
31+
32+
return obj;
33+
}
34+
}

0 commit comments

Comments
 (0)