gpt4 book ai didi

Java FX 如何 'Stop'线程中途并重新启动它或销毁它

转载 作者:行者123 更新时间:2023-12-02 01:51:08 26 4
gpt4 key购买 nike

无法停止线程中途重新启动添加节点线程。

这就是我得到的

enter image description here

这就是我需要的每当我按下刷新按钮时,我都会一遍又一遍地想要这样的东西

清理屏幕重新启动(例如。按正确顺序从 A 到 N)

enter image description here

为什么?所以我可以在刷新之前选择不同的列表。

背景故事 (忽略)我在 JavaFX 上开发一个小项目已有一个月了,但这个问题一直困扰着我。我做了研究并试图找出答案,但一无所获。如果有人引导我走上正确的道路,我真的很感激。请突出显示这个问题,这样其他人就不必浪费太多时间

我有 JavaFX Controller

@FXML public FlowPane flow_panel;

MyPlatform mp = new MyPlatform(); // A class that implements Runnable
Thread t;

boolean first_time_click = true; // To check Whether i clicked '1st' time or 'second' time
@FXML
private void Refresh_Btn(ActionEvent event)
{
System.out.println("Refresh btn");

t = new Thread(mp); //Creating Thread object

if(first_time_click == true) { first_time_click = false;



MyPlatform.stop = false; //Thread object.Stop, When false do nothing

flow_panel.getChildren().clear(); // cleaning screen
System.out.println("Start Thread");
t.setDaemon(true);
t.start(); //Start Thread


} else if(first_time_click == false){ first_time_click = true;



MyPlatform.stop = true; //Exist loop
t.interrupt(); // I thought 't.interrupt()' it work but nothing
System.out.println("Stop Thread");

flow_panel.getChildren().clear(); // cleaning screen
MyPlatform.stop = false; //Thread object.Stop, When false do nothing
t.setDaemon(true);
t.start(); //Start Thread
System.out.println("Stoped Thread Started again");


}

}
<小时/>

然后我有一个名为 MyPlatform 的类,它实现了 Runnable

import javafx.application.Platform;
import javafx.scene.control.Button;
import setups.WindowSetupConfig;

public class MyPlatform implements Runnable
{

public static boolean stop = false;

@Override
public void run()
{



for (String name : ListOfNames.getlist())
{
if(stop) // Exit for loop if true
{
break;
}
Platform.runLater(new Runnable()
{

@Override
public void run()
{

Button btn = new Button("Button : "+name);

System.out.println(name);


//Adding that button to FlowPane
WindowSetupConfig.getController().flow_panel.getChildren().add(btn);
}
});

try {
Thread.sleep(1000); // Wait for 1 sec, Controlling speed
} catch (InterruptedException e) { // try catch it needed for some reason
e.printStackTrace();
}

}


}

}
<小时/>

我有一个字符串列表。原本该列表将包含超过 500 个名字

import java.util.ArrayList;

public class ListOfNames
{
private static ArrayList<String> list_of_names ;

public ListOfNames()
{
list_of_names = new ArrayList<>();
list_of_names.add("A");
list_of_names.add("B");
list_of_names.add("C");
list_of_names.add("D");
list_of_names.add("E");
list_of_names.add("F");
list_of_names.add("G");
list_of_names.add("H");
list_of_names.add("I");
list_of_names.add("J");
list_of_names.add("K");
list_of_names.add("L");
list_of_names.add("M");
list_of_names.add("N");
}


public static ArrayList<String> getlist() {
return list_of_names;
}
}

每当我按下刷新按钮时,我想中途停止向流程面板添加点头的线程。 enter image description here

使用 flow_panel.getChildren().clear(); 清理 FlowPane;并从列表开头重新开始

enter image description here

本质上-停止-干净-重新启动

如果有任何方法可以通过服务任务来做到这一点,那就太好了

最佳答案

我没有阅读您的所有代码,但我将提供有关如何取消和“重新启动”任务的一般说明。

首先,不要使用 suspendstopresume 方法线程;它们因某种原因被弃用。

与所有后台任务一样,取消需要所述后台任务的开发人员的配合。该任务必须在适当的时间检查它是否已被取消并做出相应的 react 。一般是通过查询当前Thread的中断状态来完成的,但是Task类继承了更容易使用的isCancelled方法> future 任务

模拟长时间运行工作的示例:

public class MyTask extends Task<Void> {

@Override
protected Void call() throws Exception {
for (int i = 0; i < 10_000; i++) {
if (isCancelled()) {
break;
}
updateMessage(Integer.toString(i));
Thread.sleep(1L);
updateProgress(i, 10_000);
}
return null;
}

}

您可以使用Worker 接口(interface)的cancel 方法取消Task。注意循环每次迭代时对 isCancelled 的检查;当调用 cancel 方法时,Task 将尽早停止。此外,调用 Worker.cancel() (或 Future.cancel(true))将中断 Thread ,这意味着任何可中断的操作(例如 >Thread.sleep) 将抛出 InterruptedException

您可以通过观察Task 的属性或添加事件处理程序来对Task 成功、失败或取消使用react。

MyTask task = new MyTask();
task.setOnSucceeded(event -> handleSucceededTask());
task.setOnFailed(event -> handleFailedTask());
task.setOnCancelled(event -> handleCancelledTask());

正是在这些回调中,您可以根据需要进行清理并执行 任务。请注意,您必须启动一个新的Task,因为Task 是一次性使用的。

关于Java FX 如何 'Stop'线程中途并重新启动它或销毁它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52984343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com