Логирование смены настроек

This commit is contained in:
2025-02-12 11:53:50 +03:00
parent eb3fa2fbad
commit 548619a0e5
2 changed files with 18 additions and 7 deletions

View File

@@ -6,6 +6,8 @@ import java.util.Random;
* Симулирует переодическое изменение настроек пользователем. * Симулирует переодическое изменение настроек пользователем.
*/ */
public class Settings implements Runnable { public class Settings implements Runnable {
private Room room;
private double temperature; private double temperature;
public double getTemperature() { public double getTemperature() {
@@ -18,24 +20,33 @@ public class Settings implements Runnable {
return humidity; return humidity;
} }
public Settings(double temperature, double humidity) { public Settings(Room room, double temperature, double humidity) {
this.room = room;
this.temperature = temperature; this.temperature = temperature;
this.humidity = humidity; this.humidity = humidity;
} }
// Параметры произвольного изменения настроек температуры и влажности в комнате // Параметры произвольного изменения настроек температуры и влажности в комнате
private Random random = new Random(); private Random random = new Random();
private double temperatureMaxStep = 10; private double temperatureMaxStep = 6;
private double humidityMaxStep = 0.15; private double humidityMaxStep = 0.10;
private long maxStepTimeMs = 20000; private long maxStepTimeMs = 30000;
private void log(String string) {
System.out.printf("[Settings in room %s] %s\n", room.name, string);
}
@Override @Override
public void run() { public void run() {
while (!Thread.interrupted()) { while (!Thread.interrupted()) {
Utils.sleepRandomTime((long) (maxStepTimeMs * 0.5), maxStepTimeMs);
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;
Utils.sleepRandomTime((long) (maxStepTimeMs * 0.5), maxStepTimeMs); log(String.format(
"Changed to temperature %.2fC°, humidity %.2f%%",
temperature, humidity));
} }
} }
} }

View File

@@ -15,7 +15,7 @@ public class AppTest {
@Test @Test
void testNoDeadlock() throws InterruptedException { void testNoDeadlock() throws InterruptedException {
Room room = new Room(); Room room = new Room();
Settings settings = new Settings(28.0, 0.4); Settings settings = new Settings(room, 28.0, 0.4);
Controller controller = new Controller(room, settings); Controller controller = new Controller(room, settings);
Thread controllerThread = new Thread(controller); Thread controllerThread = new Thread(controller);
Thread roomThread = new Thread(room); Thread roomThread = new Thread(room);
@@ -48,7 +48,7 @@ public class AppTest {
@Test @Test
void testNoRaceCondition() throws InterruptedException { void testNoRaceCondition() throws InterruptedException {
Room room = new Room(); Room room = new Room();
Settings settings = new Settings(25.0, 0.4); Settings settings = new Settings(room, 25.0, 0.4);
Controller controller = new Controller(room, settings); Controller controller = new Controller(room, settings);
Thread controllerThread = new Thread(controller, "Controller-Thread"); Thread controllerThread = new Thread(controller, "Controller-Thread");
Thread roomThread = new Thread(room, "Room-Thread"); Thread roomThread = new Thread(room, "Room-Thread");