From 84b5f2464de2cdaf8c178a8bce53bfb86cf15ead Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 12 May 2022 13:38:53 +0200 Subject: [PATCH] test performance with separete thread to save and load file --- .../projekt2/turnierverwaltung/FileIO.java | 4 +-- .../TournamentDecorator.java | 26 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java index 14b76f0..8549e8a 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -81,7 +81,7 @@ public class FileIO { * @throws IOException File not readable * @throws FileNotFoundException File not found */ - public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException, FileNotFoundException { + public synchronized Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException, FileNotFoundException { if (tournamentFile == null) { logger.warning("Given tournament file is empty"); throw new IllegalArgumentException("Tournament File is null"); @@ -125,7 +125,7 @@ public class FileIO { * @throws IOException File not readable * @throws FileNotFoundException File not found */ - public void saveTournament(Tournament tournament) throws IOException, FileNotFoundException { + public synchronized void saveTournament(Tournament tournament) throws IOException, FileNotFoundException { if (tournament == null) { logger.warning("Given tournament file is empty"); throw new IllegalArgumentException("Null tournament received"); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index b0c6393..25e5bd2 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -4,11 +4,14 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class TournamentDecorator implements IsObservable{ private Tournament tournament; private FileIO fileIO; private List listener = new ArrayList<>(); + private ExecutorService executorService; public TournamentDecorator(FileIO fileIO){ setFileIO(fileIO); @@ -16,10 +19,12 @@ public class TournamentDecorator implements IsObservable{ @Override public void update() { if(tournament != null){ + saveTournament(); } } }); + executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); } public void setFileIO(FileIO fileIO) { @@ -44,14 +49,13 @@ public class TournamentDecorator implements IsObservable{ listener.remove(observer); } + public void saveTournament(){ - try { - fileIO.saveTournament(tournament); - } catch (IOException e) { - e.printStackTrace(); //TODO handle and logging - } + executorService.execute(new saveTask()); } + + public void createTournament(String name, Tournament.Type type){ if(fileIO.tournamentExists(name)){ System.out.println("Tournament with same name exists already."); @@ -133,4 +137,16 @@ public class TournamentDecorator implements IsObservable{ } } + private class saveTask implements Runnable { + + @Override + public void run() { + try { + fileIO.saveTournament(tournament); + } catch (IOException e) { + e.printStackTrace(); //TODO handle and logging + } + } + } + }