Вынес функции для сна потоков в Utils
This commit is contained in:
@@ -33,16 +33,7 @@ public class Heater implements Runnable {
|
|||||||
while (!Thread.interrupted()) {
|
while (!Thread.interrupted()) {
|
||||||
if (isOn) room.adjustTemperature(random.nextDouble() * temperatureMaxStep);
|
if (isOn) room.adjustTemperature(random.nextDouble() * temperatureMaxStep);
|
||||||
|
|
||||||
try {
|
Utils.sleepRandomTime((long) (maxStepTimeMs * 0.5), maxStepTimeMs);
|
||||||
Thread.sleep(getStepTime());
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getStepTime() {
|
|
||||||
// Спим от 0.5 * maxStepTimeMs до maxSteTimeMs миллисекунд
|
|
||||||
return (long) (random.nextDouble() * 0.5 + 0.5) * maxStepTimeMs;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,16 +37,7 @@ public class Room implements Runnable {
|
|||||||
temperature += (random.nextDouble() - 0.5) * 2 * temperatureMaxStep;
|
temperature += (random.nextDouble() - 0.5) * 2 * temperatureMaxStep;
|
||||||
humidity += (random.nextDouble() - 0.5) * 2 * humidityMaxStep;
|
humidity += (random.nextDouble() - 0.5) * 2 * humidityMaxStep;
|
||||||
|
|
||||||
try {
|
Utils.sleepRandomTime((long) (maxStepTimeMs * 0.5), maxStepTimeMs);
|
||||||
Thread.sleep(getStepTime());
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getStepTime() {
|
|
||||||
// Спим от 0.5 * maxStepTimeMs до maxSteTimeMs миллисекунд
|
|
||||||
return (long) (random.nextDouble() * 0.5 + 0.5) * maxStepTimeMs;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,11 +40,7 @@ public class Sensor implements Runnable {
|
|||||||
temperature = room.getTemperature() + (random.nextDouble() - 0.5) * 2 * maxTemperatureError;
|
temperature = room.getTemperature() + (random.nextDouble() - 0.5) * 2 * maxTemperatureError;
|
||||||
humidity = room.getHumidity() + (random.nextDouble() - 0.5) * 2 * maxHumidityError;
|
humidity = room.getHumidity() + (random.nextDouble() - 0.5) * 2 * maxHumidityError;
|
||||||
|
|
||||||
try {
|
Utils.sleep(updateIntervalMs);
|
||||||
Thread.sleep(updateIntervalMs);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
lab3/src/main/java/ru/spbstu/telematics/java/Utils.java
Normal file
21
lab3/src/main/java/ru/spbstu/telematics/java/Utils.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package ru.spbstu.telematics.java;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
static public void sleep(long millis) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(millis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static public void sleepRandomTime(long from, long to) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(ThreadLocalRandom.current().nextLong(from, to));
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user