implemented multithreading

This commit is contained in:
schrom01 2022-11-25 12:58:03 +01:00
parent e48be29d59
commit 2d88c9ea91
4 changed files with 32 additions and 10 deletions

View File

@ -1,16 +1,23 @@
package ch.zhaw.gartenverwaltung; package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader;
import ch.zhaw.gartenverwaltung.notifier.Notifier;
import javafx.application.Application; import javafx.application.Application;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
public class HelloApplication extends Application { public class HelloApplication extends Application {
@Override @Override
public void start(Stage stage) throws IOException { public void start(Stage stage) throws IOException {
AppLoader appLoader = new AppLoader(); AppLoader appLoader = new AppLoader();
Notifier notifier = new Notifier(appLoader.getGardenSchedule());
Thread notificationThread = new Thread(notifier);
notificationThread.start();
notificationThread.interrupt();
appLoader.loadSceneToStage("MainFXML.fxml", stage); appLoader.loadSceneToStage("MainFXML.fxml", stage);
stage.setTitle("Gartenverwaltung"); stage.setTitle("Gartenverwaltung");

View File

@ -10,6 +10,7 @@ import ch.zhaw.gartenverwaltung.io.TaskList;
import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.Garden;
import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.models.GardenSchedule;
import ch.zhaw.gartenverwaltung.models.PlantListModel; import ch.zhaw.gartenverwaltung.models.PlantListModel;
import ch.zhaw.gartenverwaltung.notifier.Notifier;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.DialogPane; import javafx.scene.control.DialogPane;
@ -41,6 +42,7 @@ public class AppLoader {
public AppLoader() throws IOException { public AppLoader() throws IOException {
} }
@ -154,4 +156,8 @@ public class AppLoader {
default -> null; default -> null;
}; };
} }
public GardenSchedule getGardenSchedule() {
return gardenSchedule;
}
} }

View File

@ -9,7 +9,6 @@ import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class GardenSchedule { public class GardenSchedule {

View File

@ -9,6 +9,7 @@ import java.util.function.Consumer;
public class Notifier implements Runnable{ public class Notifier implements Runnable{
private final GardenSchedule gardenSchedule; private final GardenSchedule gardenSchedule;
private boolean interrupted = false;
public Notifier(GardenSchedule gardenSchedule) { public Notifier(GardenSchedule gardenSchedule) {
this.gardenSchedule = gardenSchedule; this.gardenSchedule = gardenSchedule;
@ -18,16 +19,13 @@ public class Notifier implements Runnable{
// TODO implement // TODO implement
} }
@Override private void sendNotifications(){
public void run() { System.out.println("sending Notifications");
try { try {
gardenSchedule.getTaskList().forEach(new Consumer<Task>() { gardenSchedule.getTaskList().forEach(task -> {
@Override if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){
public void accept(Task task) { sendNotification(task);
if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){ task.setNextNotification(task.getNextExecution());
sendNotification(task);
task.setNextNotification(task.getNextExecution());
}
} }
}); });
} catch (IOException e) { } catch (IOException e) {
@ -35,4 +33,16 @@ public class Notifier implements Runnable{
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override
public void run() {
while(!interrupted){
try {
sendNotifications();
Thread.sleep(3000);
} catch (InterruptedException e) {
interrupted = true;
}
}
}
} }