Skip to content

Commit 36ac91d

Browse files
committed
Add project
1 parent 29ad2a9 commit 36ac91d

14 files changed

+263
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/description.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/project-template.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.birdegop.haircutservice;
2+
3+
public class Client {
4+
5+
private String name;
6+
7+
public Client(String name) {
8+
this.name = name;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.birdegop.haircutservice;
2+
3+
public abstract class Door {
4+
protected abstract void onPass(Client client);
5+
6+
protected HairCutService service;
7+
8+
public Door(HairCutService service) {
9+
this.service = service;
10+
}
11+
12+
public void pass(Client client) {
13+
onPass(client);
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.birdegop.haircutservice;
2+
3+
public class EntranceDoor extends Door {
4+
public EntranceDoor(HairCutService service) {
5+
super(service);
6+
}
7+
8+
@Override
9+
protected void onPass(Client client) {
10+
System.out.println("Клиент {" + client.getName() + "} Заходит");
11+
service.enter(client);
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.birdegop.haircutservice;
2+
3+
public class ExitDoor extends Door {
4+
public ExitDoor(HairCutService service) {
5+
super(service);
6+
}
7+
8+
@Override
9+
protected void onPass(Client client) {
10+
System.out.println("Клиент {" + client.getName() + "} Выходит");
11+
}
12+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.birdegop.haircutservice;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Queue;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
public class HairCutService {
9+
10+
private final Door entranceDoor;
11+
private final Door exitDoor;
12+
13+
private final HairDresser hairDresser;
14+
15+
private final Lock queueLock;
16+
17+
private final Queue<Client> clients;
18+
19+
private int maxQueueSize;
20+
21+
public HairCutService(int maxQueueSize) {
22+
entranceDoor = new EntranceDoor(this);
23+
exitDoor = new ExitDoor(this);
24+
hairDresser = new HairDresser(this);
25+
clients = new ArrayDeque<>();
26+
queueLock = new ReentrantLock();
27+
this.maxQueueSize = maxQueueSize;
28+
}
29+
30+
public Lock getQueueLock() {
31+
return queueLock;
32+
}
33+
34+
public Queue<Client> getClients() {
35+
return clients;
36+
}
37+
38+
public void enter(Client client) {
39+
if (clients.size() == maxQueueSize) {
40+
System.out.println("Больше нет свободных мест");
41+
exit(client);
42+
} else {
43+
clients.add(client);
44+
hairDresser.notifyClient();
45+
}
46+
}
47+
48+
public void exit(Client client) {
49+
exitDoor.pass(client);
50+
}
51+
52+
public Door getEntranceDoor() {
53+
return entranceDoor;
54+
}
55+
56+
public Door getExitDoor() {
57+
return exitDoor;
58+
}
59+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.birdegop.haircutservice;
2+
3+
import java.util.Queue;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
public class HairDresser {
8+
9+
boolean shouldContinue;
10+
HairCutService service;
11+
Thread thread;
12+
Lock wakeupLock;
13+
boolean wakeup = false;
14+
15+
public HairDresser(HairCutService service) {
16+
this.service = service;
17+
shouldContinue = true;
18+
thread = new Thread(this::job);
19+
wakeupLock = new ReentrantLock(true);
20+
thread.start();
21+
}
22+
23+
public void terminate() {
24+
shouldContinue = false;
25+
}
26+
27+
public void notifyClient() {
28+
try {
29+
wakeupLock.lock();
30+
wakeup = true;
31+
wakeupLock.unlock();
32+
} catch (Exception ignored) {
33+
34+
}
35+
}
36+
37+
public void job() {
38+
boolean lastStatus = wakeup;
39+
while (shouldContinue) {
40+
wakeupLock.lock();
41+
if (wakeup) {
42+
if (lastStatus != wakeup) {
43+
System.out.println("Парикмахер проснулся");
44+
lastStatus = wakeup;
45+
}
46+
Queue<Client> clients = service.getClients();
47+
if (!clients.isEmpty()) {
48+
Lock queueLock = service.getQueueLock();
49+
queueLock.lock();
50+
Client client = clients.poll();
51+
queueLock.unlock();
52+
53+
make(client);
54+
}
55+
if (clients.isEmpty()) {
56+
wakeup = false;
57+
System.out.println("Парикмахер уснул");
58+
}
59+
}
60+
wakeupLock.unlock();
61+
}
62+
}
63+
64+
public void make(Client client) {
65+
System.out.println("Парикмахер обслуживает клиента {" + client.getName() + "}");
66+
try {
67+
Thread.sleep(1000);
68+
} catch (InterruptedException e) {
69+
e.printStackTrace();
70+
}
71+
releaseClient(client);
72+
}
73+
74+
private void releaseClient(Client client) {
75+
service.exit(client);
76+
}
77+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.birdegop.haircutservice;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
7+
HairCutService service = new HairCutService(3);
8+
9+
for (int i = 0; i < 4; i++) {
10+
service.getEntranceDoor().pass(new Client("Cl" + i));
11+
}
12+
13+
14+
try {
15+
Thread.sleep(5000);
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
20+
//service.getEntranceDoor().pass(new Client("Mike"));
21+
}
22+
}

untitled.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+

0 commit comments

Comments
 (0)