File tree Expand file tree Collapse file tree
src/main/java/ru/nsu/gunko/topic9/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.topic9.task1</groupId >
8+ <artifactId >topic9</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 .topic9 .task1 ;
2+
3+ public class CommonRes {
4+ private int thread ;
5+ private int string ;
6+
7+ public CommonRes () {
8+ thread = 1 ;
9+ string = 1 ;
10+ }
11+
12+ public int getString () {
13+ return string ;
14+ }
15+
16+ public int getThread () {
17+ return thread ;
18+ }
19+
20+ public void addString () {
21+ ++string ;
22+ }
23+
24+ public void addThread () {
25+ ++thread ;
26+ }
27+
28+ public void begThread () {
29+ thread = 1 ;
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package ru .nsu .gunko .topic9 .task1 ;
2+
3+ public class Handler implements Runnable {
4+ private final Printer printer ;
5+ private final CommonRes commonRes ;
6+ private final int M ;
7+
8+ public Handler (int M , Printer printer , CommonRes commonRes ) {
9+ this .printer = printer ;
10+ this .commonRes = commonRes ;
11+ this .M = M ;
12+ }
13+
14+ @ Override
15+ public void run () {
16+ String name = Thread .currentThread ().getName ();
17+ int numThread = Integer .parseInt (name .substring (name .length ()-1 ));
18+ int i = 0 ;
19+
20+ while (i < M ) {
21+ if (numThread == commonRes .getThread ()) {
22+ printer .print (numThread );
23+ ++i ;
24+ } else {
25+ printer .myWait ();
26+ }
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package ru .nsu .gunko .topic9 .task1 ;
2+
3+ import java .util .concurrent .*;
4+
5+ public class Main {
6+ public static void main (String [] args ) {
7+ int N = Integer .parseInt (args [0 ]),
8+ M = Integer .parseInt (args [1 ]);
9+ CommonRes commonRes = new CommonRes ();
10+ Printer printer = new Printer (N , commonRes );
11+
12+ try (ExecutorService service = Executors .newCachedThreadPool ()) {
13+ for (int i = 0 ; i < N ; ++i ) {
14+ service .execute (new Handler (M , printer , commonRes ));
15+ }
16+ }
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package ru .nsu .gunko .topic9 .task1 ;
2+
3+ public class Printer {
4+ private final CommonRes commonRes ;
5+ private final int N ;
6+
7+ public Printer (int N , CommonRes commonRes ) {
8+ this .commonRes = commonRes ;
9+ this .N = N ;
10+ }
11+
12+ public synchronized void print (int numThread ) {
13+ System .out .println (numThread +"-" +commonRes .getString ());
14+
15+ synchronized (commonRes ) {
16+ if (numThread == N ) {
17+ commonRes .begThread ();
18+ commonRes .addString ();
19+ } else {
20+ commonRes .addThread ();
21+ }
22+ }
23+
24+ notifyAll ();
25+ }
26+
27+ public synchronized void myWait () {
28+ try {
29+ wait (100 );
30+ } catch (InterruptedException e ) {
31+ throw new RuntimeException (e );
32+ }
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments