test performance with separete thread to save and load file
This commit is contained in:
parent
d3a4b4cc87
commit
84b5f2464d
|
@ -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");
|
||||
|
|
|
@ -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<IsObserver> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue