Triathlon程序执行一个长时间运行的任务,如果该任务已完全执行,则有可能重新启动该任务。我想添加停止执行以重置UI的可能性。为了达到这个目的,我增加了一个新的按钮,停止。代码如下:
package triathlon2;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.*;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.concurrent.Task;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Triathlon2 extends Application
{
private final Random random = new Random();
private final ExecutorService exec = Executors.newSingleThreadExecutor();
final TaskMonitor taskMonitor = new TaskMonitor();
final ProgressIndicator progressIndicator
= new ProgressIndicator();
@Override public void start(Stage stage) throws Exception
{
progressIndicator.progressProperty()
.bind(taskMonitor.currentTaskProgressProperty());
final Label currentRaceStage = new Label();
currentRaceStage.textProperty()
.bind(taskMonitor.currentTaskNameProperty());
createMainLayout(
stage,
createStartRaceButton(
exec,
taskMonitor
),
createStopButton(
//exec,
taskMonitor
),
createRaceProgressView(
taskMonitor,
progressIndicator,
currentRaceStage
)
);
}
@Override public void stop() throws Exception
{
exec.shutdownNow();
}
private Button createStartRaceButton(
final ExecutorService exec,
final TaskMonitor taskMonitor)
{
final Button startButton = new Button("Start Race");
startButton.disableProperty()
.bind(taskMonitor.idleProperty().not());
startButton.setOnAction((ActionEvent actionEvent) ->
{
runRace(exec, taskMonitor);
});
return startButton;
}
private Button createStopButton(
//final ExecutorService exec,
final TaskMonitor taskMonitor)
{
final Button stopButton = new Button("Stop Race");
stopButton.disableProperty()
.bind(taskMonitor.idleProperty());
stopButton.setOnAction((ActionEvent actionEvent) ->
{
exec.shutdownNow();
Platform.setImplicitExit(true);
});
return stopButton;
}
private HBox createRaceProgressView(
final TaskMonitor taskMonitor,
ProgressIndicator progressIndicator,
Label currentRaceStage)
{
final HBox raceProgress = new HBox(10);
raceProgress.getChildren().setAll(
currentRaceStage,
progressIndicator
);
raceProgress.setOpacity(0);
raceProgress.setAlignment(Pos.CENTER);
final FadeTransition fade
= new FadeTransition(
Duration.seconds(0.75), raceProgress);
fade.setToValue(0);
taskMonitor.idleProperty()
.addListener((Observable observable) ->
{
if (taskMonitor.idleProperty().get())
{
fade.playFromStart();
} else
{
fade.stop();
raceProgress.setOpacity(1);
}
});
return raceProgress;
}
private void createMainLayout(Stage stage, Button startButton, Button stopButton, HBox raceProgress)
{
final VBox layout = new VBox(10);
layout.getChildren().setAll(
raceProgress,
startButton,stopButton
);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10px;");
stage.setScene(new Scene(layout, 500, 600));
stage.show();
}
private void runRace(ExecutorService exec, TaskMonitor taskMonitor)
{
StageTask swimTask = new StageTask("Swim", 30, 40);
StageTask bikeTask = new StageTask("Bike", 210, 230);
StageTask runTask = new StageTask("Run", 120, 140);
taskMonitor.monitor(swimTask, bikeTask, runTask);
exec.execute(swimTask);
exec.execute(bikeTask);
exec.execute(runTask);
}
class TaskMonitor {
final private ReadOnlyObjectWrapper<StageTask> currentTask = new ReadOnlyObjectWrapper<>();
final private ReadOnlyStringWrapper currentTaskName = new ReadOnlyStringWrapper();
final private ReadOnlyDoubleWrapper currentTaskProgress = new ReadOnlyDoubleWrapper();
final private ReadOnlyBooleanWrapper idle = new ReadOnlyBooleanWrapper(true);
public void monitor(final StageTask task)
{
task.stateProperty().addListener(new ChangeListener<Task.State>()
{
@Override
public void changed(ObservableValue<? extends Task.State> observableValue, Task.State oldState, Task.State state)
{
switch (state)
{
case RUNNING:
currentTask.set(task);
currentTaskProgress.unbind();
currentTaskProgress.set(task.progressProperty().get());
currentTaskProgress.bind(task.progressProperty());
currentTaskName.set(task.nameProperty().get());
idle.set(false);
break;
case SUCCEEDED:
case CANCELLED:
case FAILED:
task.stateProperty().removeListener(this);
idle.set(true);
break;
}
}
});
}
public void monitor(final StageTask... tasks)
{
for (StageTask task: tasks) {
monitor(task);
}
}
public ReadOnlyObjectProperty<StageTask> currentTaskProperty()
{
return currentTask.getReadOnlyProperty();
}
public ReadOnlyStringProperty currentTaskNameProperty()
{
return currentTaskName.getReadOnlyProperty();
}
public ReadOnlyDoubleProperty currentTaskProgressProperty()
{
return currentTaskProgress.getReadOnlyProperty();
}
public ReadOnlyBooleanProperty idleProperty()
{
return idle.getReadOnlyProperty();
}
}
class StageTask extends Task<Duration>
{
final private ReadOnlyStringWrapper name;
final private int minMinutesElapsed;
final private int maxMinutesElapsed;
public StageTask(String name, int minMinutesElapsed, int maxMinutesElapsed)
{
this.name = new ReadOnlyStringWrapper(name);
this.minMinutesElapsed = minMinutesElapsed;
this.maxMinutesElapsed = maxMinutesElapsed;
}
@Override protected Duration call() throws Exception
{
Duration duration = timeInRange(
minMinutesElapsed, maxMinutesElapsed
);
for (int i = 0; i < 25; i++)
{
updateProgress(i, 25);
Thread.sleep((int) (duration.toMinutes()));
}
updateProgress(25, 25);
return duration;
}
private Duration timeInRange(int min, int max)
{
return Duration.minutes(
random.nextDouble() * (max - min) + min
);
}
public ReadOnlyStringProperty nameProperty()
{
return name.getReadOnlyProperty();
}
}
public static void main(String[] args)
{
Application.launch(Triathlon2.class);
}
}
如果任务已经完成,程序很好地重新启动,但是如果我在停止它之后调用start,程序就会崩溃。我该纠正什么?
这里出现的基本逻辑错误是,在执行器
上启动shutdown
之后,试图将任务提交给该执行器。从Java的文件
启动有序关闭,其中执行以前提交的任务,但不接受新任务
因为我们正在为exec
调用shutdownnow()
方法,单击stop race
按钮。在此之后,exec
将不能接受任何进一步的任务。
附注。因为exec
是最终的,所以不能重新实例化它!
有没有你可以开始和停止一个动画,所以播放1秒,停止1秒?我尝试过用单选按钮与Thread.Sleep切换来实现这一点,但是我认为这种方式是不可能的。有没有其他方法可以做到这一点?多谢了。
问题内容: 我已经从http://jenkins-ci.org/content/thank-you-downloading-windows- installer 下载了“ jenkins-1.501.zip” 。 我已经解压缩了zip文件,并在Windows 7上成功安装了Jenkins。詹金斯的表现不错。我想从控制台停止Jenkins服务。我怎样才能做到这一点?通过控制台/命令行启动和重新启动的
问题内容: 我很难找到一种方法来启动,停止和重新启动Java中的线程。 具体来说,我在中有一个类Task(当前实现)。我的主应用程序需要能够在线程上启动此任务,在需要时停止(杀死)该线程,有时还可以杀死并重新启动该线程… 我的第一次尝试是与,但我似乎找不到办法重新启动任务。当我使用任何将来的呼叫失败时,因为是“关机” … 那么,我该怎么做呢? 问题答案: 一旦线程停止,你将无法重新启动它。但是,没
参考 workerman手册 http://doc3.workerman.net/install/start-and-stop.html
注意Workerman启动停止等命令都是在命令行中完成的。 要启动Workerman,首先需要有一个启动入口文件,里面定义了服务监听的端口及协议。可以参考入门指引--简单开发实例部分 这里以workerman-chat为例,它的启动入口为start.php。 启动 以debug(调试)方式启动 php start.php start 以daemon(守护进程)方式启动 php start.php