From eef0b2981f66728088f81ce1ce85dcaaed22e9c3 Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Sun, 8 Mar 2020 11:41:49 +0100 Subject: [PATCH] Experimental Thread pausing. TODO: test. --- src/com/olexyn/ensync/Flow.java | 17 ++++++++++++---- src/com/olexyn/ensync/Main.java | 8 +++----- src/com/olexyn/ensync/ui/Controller.java | 25 ++++++++++++++++++------ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/com/olexyn/ensync/Flow.java b/src/com/olexyn/ensync/Flow.java index 3c77a46..68a7a97 100644 --- a/src/com/olexyn/ensync/Flow.java +++ b/src/com/olexyn/ensync/Flow.java @@ -10,7 +10,7 @@ public class Flow implements Runnable{ - + private String state; public void run() { @@ -20,14 +20,16 @@ public class Flow implements Runnable{ for (Map.Entry mapEntry : Main.sync.syncMaps.entrySet()) { + SyncMap syncMap = mapEntry.getValue(); + state = syncMap.toString(); - - for (Map.Entry entry : mapEntry.getValue().syncDirectories.entrySet()) { + for (Map.Entry entry : syncMap.syncDirectories.entrySet()) { SyncDirectory syncDirectory = entry.getValue(); String path = syncDirectory.path; String stateFilePath = syncDirectory.stateFilePath(path); if (new File(stateFilePath).exists()) { + state = "READ-STATE-FILE-"+ syncDirectory.readStateFile(); } else { syncDirectory.writeStateFile(path); @@ -43,6 +45,7 @@ public class Flow implements Runnable{ String path = syncDirectory.path; + state = "READ"; syncDirectory.readState(); syncDirectory.makeListCreated(); @@ -57,11 +60,13 @@ public class Flow implements Runnable{ syncDirectory.writeStateFile(path); + + } try { System.out.println("Pausing..."); - Thread.sleep(1000); + Main.flowThread.sleep(2000); } catch (InterruptedException e) { } @@ -69,4 +74,8 @@ public class Flow implements Runnable{ } } + + public String getState() { + return state==null ? "NONE" : state; + } } diff --git a/src/com/olexyn/ensync/Main.java b/src/com/olexyn/ensync/Main.java index e29643e..a33e784 100644 --- a/src/com/olexyn/ensync/Main.java +++ b/src/com/olexyn/ensync/Main.java @@ -22,11 +22,11 @@ public class Main{ final public static Sync sync = new Sync(); - final private static Flow flow = new Flow(); - final private static UI ui = new UI(); + final public static Thread uiThread = new Thread(new UI(), "ui"); + final public static Thread flowThread = new Thread(new Flow(), "flow"); @@ -37,11 +37,9 @@ public class Main{ sync.syncMaps.put("test", new SyncMap("test")); - Thread uiThread = new Thread(ui, "ui"); - uiThread.start(); + uiThread.start(); - Thread flowThread = new Thread(flow, "flow"); flowThread.start(); diff --git a/src/com/olexyn/ensync/ui/Controller.java b/src/com/olexyn/ensync/ui/Controller.java index 614d035..0b5b578 100644 --- a/src/com/olexyn/ensync/ui/Controller.java +++ b/src/com/olexyn/ensync/ui/Controller.java @@ -33,7 +33,7 @@ public class Controller implements Initializable { Button addDirButton = new Button("Add"); addDirButton.setId("addDirButton"); - addDirButton.setOnAction(event -> { this.addDir();}); + addDirButton.setOnAction(event -> { this.addDirectory();}); gridPane.add(directoryField, 0, 0); gridPane.add(new Text(""), 1, 0); @@ -49,7 +49,7 @@ public class Controller implements Initializable { protected GridPane gridPane; - protected void addDir() { + protected void addDirectory() { Window stage = gridPane.getScene().getWindow(); final DirectoryChooser directoryChooser = new DirectoryChooser(); @@ -73,7 +73,7 @@ public class Controller implements Initializable { Button removeButton = new Button("Remove"); removeButton.setId("removeButton" + dir.getAbsolutePath()); - removeButton.setOnAction(event -> { this.rmDir(removeButton.getId());}); + removeButton.setOnAction(event -> { this.removeDirectory(removeButton.getId());}); List nodeList = new ArrayList<>(); @@ -118,10 +118,8 @@ public class Controller implements Initializable { } - private void rmDir(String id) { - String path = id.replace("removeButton", ""); + private void removeDirectory(String id) { - Main.sync.syncMaps.get("test").removeDirectory(path); List nodeList = new ArrayList<>(); @@ -156,6 +154,21 @@ public class Controller implements Initializable { } + + String path = id.replace("removeButton", ""); + while(true){ + if (Main.flowThread.getState().equals(Thread.State.TIMED_WAITING)){ + try { + Main.flowThread.wait(); + } catch (InterruptedException e){ + Main.sync.syncMaps.get("test").removeDirectory(path); + Main.flowThread.notify(); + break; + } + } + } + + }