package com.company;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
public static int zt = 0;
public static void main(String[] args) {
Application.launch(Main.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Worker worker = new Worker();
worker.setDaemon(true);
worker.start();
Button button = new Button("开始移动");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
zt++;
button.setText("正在移动...");
if (zt % 2 == 1) {
System.out.println("开始运行了");
synchronized (worker) {
worker.setPaused(false);
worker.notify();
}
} else {
System.out.println("暂停了");
button.setText("开始移动");
worker.setPaused(true);
}
}
});
AnchorPane a5 = new AnchorPane();
a5.getChildren().add(button);
Scene scene = new Scene(a5);
primaryStage.setScene(scene);
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.setTitle("移动文件");
primaryStage.show();
}
}
class Worker extends Thread {
private volatile boolean isPaused = true;
public void setPaused(boolean paused) {
isPaused = paused;
}
@Override
public void run() {
while (true) {
synchronized (this) {
while (isPaused) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(123);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}