Я получаю систему println переменной "j" до десяти раз подряд, но я не вижу никакой причины для этого.
Вот мой код:
copyFolderButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String dateStart = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
j = 0.00;
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.schedule(new Runnable() {
@Override
public void run() {
List<String> finallist = copyList.getItems();
totalComputersCopy = copyList.getItems().size();
successRateCopy = 0;
double a = 1.00;
double i = (1.00 / finallist.size());
ArrayList<String> selectedIDs = new ArrayList<String>();
try {
for (String row : finallist) {
a++;
j = j + i;
if (a % 10 == 0) {
Thread.sleep(1500);
}
System.out.println(a);
copythread = new Thread("" + a) {
public void run() {
try {
System.out.println(j);
javafx.application.Platform.runLater(() -> copyGauge.setValue(j * 100));
} catch (Exception ex) {
}
Process p;
try {
String pathRaw = copyFolderPath.getText();
pathRaw = pathRaw.replace(":", "$");
if (pathRaw != null && pathRaw.length() > 0 && pathRaw.charAt(pathRaw.length() - 1) == '\\') {
pathRaw = pathRaw.substring(0, pathRaw.length() - 1);
}
String source = copyFolderSource.getText();
if (source != null && source.length() > 0 && source.charAt(source.length() - 1) == '\\') {
source = source.substring(0, source.length() - 1);
}
InetAddress ia = InetAddress.getByName("172.217.23.206");
boolean b = ia.isReachable(2000);
if (b) {
//System.out.println(j*100);
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "powershell -Command \"Copy-Item '" + source + "' -Destination '" + "\\\\" + row + "\\" + pathRaw + "' -Recurse -Verbose\"");
p = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuilder builders = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builders.append(line);
builders.append("\n");
}
String DeleteLog = builders.toString();
if (DeleteLog.trim().isEmpty()) {
successRateCopy++;
javafx.application.Platform.runLater(() -> copyFolderLog.appendText(row + ": " + "Succesfully copied" + "\n"));
} else {
javafx.application.Platform.runLater(() -> copyFolderLog.appendText(row + ": " + DeleteLog + "\n"));
}
p.waitFor(15, TimeUnit.SECONDS);
p.destroy();
} else {
javafx.application.Platform.runLater(() -> copyFolderLog.appendText("Cannot reach " + row + "\n"));
System.out.println("Cannot reach " + row + "\n");
}
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.toString());
} catch (InterruptedException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
Thread.currentThread().interrupt();
}
}
};
copythread.start();
}
} catch (Exception e) {
} finally {
while (copythread.getState() != Thread.State.TERMINATED) {
}
String successRateLog = String.valueOf(successRateCopy);
String failedRateLog = String.valueOf(totalComputersCopy - successRateCopy);
String dateStop = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
if ((diff / (60 * 60 * 1000) % 24) != 0) {
deleteTime = "Total time: " + String.valueOf(diffHours) + " hours, " + String.valueOf(diffMinutes) + " minutes, " + String.valueOf(diffSeconds) + " seconds";
} else {
if ((diff / (60 * 1000) % 60) != 0) {
deleteTime = "Total time: " + String.valueOf(diffMinutes) + " minutes, " + String.valueOf(diffSeconds) + " seconds";
} else {
if ((diff / 1000 % 60) != 0) {
deleteTime = "Total time: " + String.valueOf(diffSeconds) + " seconds";
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
Platform.runLater(() -> {
VBox container = new VBox();
container.setPadding(new Insets(10, 10, 10, 10));
Label icon = new Label();
Image image = new Image(getClass().getResourceAsStream("img/copyicon.png"));
icon.setGraphic(new ImageView(image));
container.getChildren().add(icon);
Notifications.create()
.darkStyle()
.title("Copy Folder")
.text("Workcomplete!" + "\n" + deleteTime + "\n" + "Success: " + successRateLog + "\n" + "Failed: " + failedRateLog)
.hideAfter(Duration.seconds(30))
.graphic(container)
.show();
}
);
}
}
}, 1, TimeUnit.SECONDS);
}
});
Я не уверен, что это лучший способ игры с потоками.Это работает хорошо, но я знаю, что не так с печатью переменных в моем коде, это выглядит так:
2.0
3.0
4.0
5.0
6.0
0.03333333333333333
7.0
0.04
0.04
0.04
0.04666666666666667
8.0
9.0
0.060000000000000005
0.060000000000000005
0.060000000000000005
10.0
11.0
12.0
13.0
14.0
15.0
16.0
17.0
18.0
19.0
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
«A» печатает нормально, но «j» увеличивается до десяти строк и сохраняет их.