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;
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");

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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<Task>() {
@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;
}
}
}
}