-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
177 lines (143 loc) · 5.73 KB
/
Solution.java
File metadata and controls
177 lines (143 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//All things whatever the Father has are mine; therefore I said that he takes of mine, and will declare it to you. (John 16:15)
package com.javarush.task.task16.task1615;
/*
Аэропорт
*/
public class Solution {
public static volatile Runway RUNWAY = new Runway(); //1 взлетная полоса
public static void main(String[] args) throws InterruptedException {
Plane plane1 = new Plane("Самолет #1");
Plane plane2 = new Plane("Самолет #2");
Plane plane3 = new Plane("Самолет #3");
}
private static void waiting() {
try { //add your code here - добавь код тут
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void takingOff() {
//fix this method - исправь этот метод
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
public static class Plane extends Thread {
public Plane(String name) {
super(name);
start();
}
public void run() {
boolean isAlreadyTakenOff = false;
while (!isAlreadyTakenOff) {
if (RUNWAY.trySetTakingOffPlane(this)) { //если взлетная полоса свободна, занимаем ее
System.out.println(getName() + " взлетает");
takingOff();//взлетает
System.out.println(getName() + " уже в небе");
isAlreadyTakenOff = true;
RUNWAY.setTakingOffPlane(null);
} else if (!this.equals(RUNWAY.getTakingOffPlane())) { //если взлетная полоса занята самолетом
System.out.println(getName() + " ожидает");
waiting(); //ожидает
}
}
}
}
public static class Runway { //взлетная полоса
private Thread t;
public Thread getTakingOffPlane() {
return t;
}
public void setTakingOffPlane(Thread t) {
synchronized (this) {
this.t = t;
}
}
public boolean trySetTakingOffPlane(Thread t) {
synchronized (this) {
if (this.t == null) {
this.t = t;
return true;
}
return false;
}
}
}
}
/*
Аэропорт
1. Разберись, что делает программа.
2. Исправь метод takingOff(взлет) — сейчас он работает оооочень долго. Взлет должен занимать 100 миллисекунд.
3. Реализуй метод waiting по аналогии с методом takingOff. Время ожидания не должно превышать время взлета.
Требования:
1. Метод takingOff должен работать примерно 100 мс.
2. Метод waiting должен работать примерно 100 мс.
3. В методе main должно создаваться 3 самолета.
4. В классе Solution должен быть вложенный класс Plane (самолет).
5. В классе Solution должен быть вложенный класс Runway (взлетная полоса).
package com.javarush.task.task16.task1615;
*
Аэропорт
*
public class Solution {
public static volatile Runway RUNWAY = new Runway(); //1 взлетная полоса
public static void main(String[] args) throws InterruptedException {
Plane plane1 = new Plane("Самолет #1");
Plane plane2 = new Plane("Самолет #2");
Plane plane3 = new Plane("Самолет #3");
}
private static void waiting() {
//add your code here - добавь код тут
}
private static void takingOff() {
//fix this method - исправь этот метод
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
public static class Plane extends Thread {
public Plane(String name) {
super(name);
start();
}
public void run() {
boolean isAlreadyTakenOff = false;
while (!isAlreadyTakenOff) {
if (RUNWAY.trySetTakingOffPlane(this)) { //если взлетная полоса свободна, занимаем ее
System.out.println(getName() + " взлетает");
takingOff();//взлетает
System.out.println(getName() + " уже в небе");
isAlreadyTakenOff = true;
RUNWAY.setTakingOffPlane(null);
} else if (!this.equals(RUNWAY.getTakingOffPlane())) { //если взлетная полоса занята самолетом
System.out.println(getName() + " ожидает");
waiting(); //ожидает
}
}
}
}
public static class Runway { //взлетная полоса
private Thread t;
public Thread getTakingOffPlane() {
return t;
}
public void setTakingOffPlane(Thread t) {
synchronized (this) {
this.t = t;
}
}
public boolean trySetTakingOffPlane(Thread t) {
synchronized (this) {
if (this.t == null) {
this.t = t;
return true;
}
return false;
}
}
}
}
*/