From 2d88c9ea9108449d3d60f60285cb5bfec54f2945 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 25 Nov 2022 12:58:03 +0100 Subject: [PATCH] implemented multithreading --- .../gartenverwaltung/HelloApplication.java | 7 +++++ .../gartenverwaltung/bootstrap/AppLoader.java | 6 ++++ .../models/GardenSchedule.java | 1 - .../gartenverwaltung/notifier/Notifier.java | 28 +++++++++++++------ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java index 9018a31..0b8a0a1 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java @@ -1,16 +1,23 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; +import ch.zhaw.gartenverwaltung.notifier.Notifier; import javafx.application.Application; import javafx.stage.Stage; import java.io.IOException; public class HelloApplication extends Application { + @Override public void start(Stage stage) throws IOException { AppLoader appLoader = new AppLoader(); + Notifier notifier = new Notifier(appLoader.getGardenSchedule()); + Thread notificationThread = new Thread(notifier); + notificationThread.start(); + notificationThread.interrupt(); + appLoader.loadSceneToStage("MainFXML.fxml", stage); stage.setTitle("Gartenverwaltung"); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index 59a4f7d..6c660ab 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -10,6 +10,7 @@ import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.models.PlantListModel; +import ch.zhaw.gartenverwaltung.notifier.Notifier; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.DialogPane; @@ -41,6 +42,7 @@ public class AppLoader { public AppLoader() throws IOException { + } @@ -154,4 +156,8 @@ public class AppLoader { default -> null; }; } + + public GardenSchedule getGardenSchedule() { + return gardenSchedule; + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index f4b2bb3..936eaf6 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -9,7 +9,6 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.function.Consumer; import java.util.stream.Collectors; public class GardenSchedule { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java index 857fa1d..26e22fc 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java @@ -9,6 +9,7 @@ import java.util.function.Consumer; public class Notifier implements Runnable{ private final GardenSchedule gardenSchedule; + private boolean interrupted = false; public Notifier(GardenSchedule gardenSchedule) { this.gardenSchedule = gardenSchedule; @@ -18,16 +19,13 @@ public class Notifier implements Runnable{ // TODO implement } - @Override - public void run() { + private void sendNotifications(){ + System.out.println("sending Notifications"); try { - gardenSchedule.getTaskList().forEach(new Consumer() { - @Override - public void accept(Task task) { - if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){ - sendNotification(task); - task.setNextNotification(task.getNextExecution()); - } + gardenSchedule.getTaskList().forEach(task -> { + if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){ + sendNotification(task); + task.setNextNotification(task.getNextExecution()); } }); } catch (IOException e) { @@ -35,4 +33,16 @@ public class Notifier implements Runnable{ e.printStackTrace(); } } + + @Override + public void run() { + while(!interrupted){ + try { + sendNotifications(); + Thread.sleep(3000); + } catch (InterruptedException e) { + interrupted = true; + } + } + } }