File tree Expand file tree Collapse file tree
src/main/java/ru/nsu/gunko/topic7/task1 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments