JAVAFX:线程停止/启动

时间:2018-02-27 14:59:29

标签: java multithreading javafx-8

我的应用目前正在连接到网络服务,以获取在应用中使用的数据。我试图对此进程进行处理,以便每隔X秒触发一次getWebServiceData()方法。我认为我的问题是我尝试使用相同的WebServiceConnect实例重新启动新线程,但我想不出任何其他方式...任何人都可以帮我构建这个,以便复选框可以终止线程,然后在重新调整时创建一个新的?

我可以轻松启动线程,一旦终止它就重新启动它。我想用一个复选框来实现这个目的:

WebServiceConnect类:

public class WebServiceConnect extends Thread {

    private Boolean runThread = true;

    public Boolean getRunThread() {
        return runThread;
    }

    public void setRunThread(Boolean runThread) {
        this.runThread = runThread;
    }

    //the run method comes from the thread class.
    //allows code to be ran in the background.
    public void run() {
        System.out.println("Starting web service thread......");
        try {
            Thread.sleep(6000);
            while(runThread) {
                getWebServiceData();
                System.out.println("The data has been refreshed.");
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(WebServiceConnect.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("terminating thread.....");
            return;
        } catch (IOException ex) {
            Logger.getLogger(WebServiceConnect.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

主类:

public class CourseworkThree extends Application {

    private static final List<String> strings = Arrays.asList("2011", "2012", "2013");
    private BarChart BarChart1;
    private CheckBox[] checkBoxesYear;
    WebServiceConnect wsc = new WebServiceConnect();

    @Override
    public void start(Stage primaryStage) throws IOException {
        wsc.start();

        HBox HBox1 = new HBox();
        HBox1.setPadding(new Insets(10, 10, 10, 10));
        HBox1.setSpacing(10);

        //A checkbox with a string caption
        CheckBox cb2 = new CheckBox("Refresh Data?");
        cb2.setSelected(true);
        HBox1.getChildren().add(cb2);

        cb2.setOnAction((event) -> {
            if(!cb2.isSelected()) {
                wsc.setRunThread(false);
                wsc.interrupt();
                System.out.println("You have stopped refreshing data automatically.");
            } else {
                wsc.setRunThread(true);
                wsc.start();

            }
        }); 

1 个答案:

答案 0 :(得分:2)

Java FX提供处理此类事物的服务。

例如。

private class KeepSessionAliveService extends ScheduledService<Void> {
        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws WhatEverException {
                    //Call your code.
                    return null;
                }
            };
        }
    }

然后设定你的时间。

private void startPolling() {
        service = new KeepSessionAliveService() ;
        service.setPeriod(Duration.minutes(6));
        service.setDelay(Duration.minutes(6)); //If you want to wait before starting it.

        service.setOnSucceeded(event -> Platform.runLater(() -> {
            //reset back to FX Thread??
        }));

        service.start();
    }

这将安排您的线程以预定义的间隔运行。它将运行FX线程,因此它不会影响您的UI。如果您关闭应用程序,服务将关闭它自己的应用程序。我在我的应用程序中使用它来保持与服务器的会话活动 - 就像一个魅力。