JavaFX 在不使用 Thread.Sleep() 的情况下在 for 循环期间暂停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23920446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:03:31  来源:igfitidea点击:

JavaFX pausing during for loop WITHOUT using Thread.Sleep()

javafor-loopjavafxfxmlthread-sleep

提问by user3513071

I'm coding this in JavaFX using a FXMLController and the scene builder. I'm currently creating an Instagram "liker" mostly just for practice as I'm a Computer Science major, and I just like coding.

我正在使用 FXMLController 和场景构建器在 JavaFX 中对此进行编码。我目前正在创建一个 Instagram“喜欢者”,主要是为了练习,因为我是计算机科学专业的,我只是喜欢编码。

My issue is, I have a for loop that "likes" 20 photos for each hashtag, I obviously need a delay or I'd get banned rather quickly. The user can choose the delay, then I randomize this delay so it's not too robotic.

我的问题是,我有一个 for 循环,每个主题标签“喜欢”20 张照片,我显然需要延迟,否则我会很快被禁止。用户可以选择延迟,然后我随机化这个延迟,这样它就不会太机械了。

The only way I've been able to use this delay is through the thread.sleep method, but since I'm coding on the FXMLController, it freezes the whole UI, and somehow stops other textareas from being updated. This is obviously not ideal.

我能够使用此延迟的唯一方法是通过 thread.sleep 方法,但由于我在 FXMLController 上编码,它冻结了整个 UI,并以某种方式阻止其他 textarea 更新。这显然不理想。

I've looked everywhere trying to find an alternative to this, but I can't find anything that works in my program.

我到处寻找替代方案,但我找不到任何适用于我的程序的东西。

I'll give the whole method:

我将给出整个方法:

    private void start() {
    sleepWhen();
    tagNumber++;
    int delays = Integer.valueOf(delay.getText());
    delays = delays * 1000;
    if (loaded) {
        runTime.clear();
        runTime.appendText("Liking...");
        for (int i = 0; i < 20; i++) {
            webEngine.executeScript("document.getElementsByClassName('btn btn-default btn-xs likeButton')[" + i + "].click()");
            try {
                double random = Math.random() * 1000;
                random = random + delays;
                delays = (int) random;
                System.out.println(delays);
                likes++;
                likesNumber.clear();
                likesNumber.appendText(String.valueOf(likes));
                System.out.println(String.valueOf(likes));
                Thread.sleep(delays);
                delays = Integer.valueOf(delay.getText());
                delays = delays * 1000;

            } catch (InterruptedException ex) {
                //do nothing
            }

            System.out.println("tag number" + tagNumber + "numbertags" + numberTags);
            if (!sleep) {
                if (tagNumber < numberTags) {
                    System.out.println("Loading tag:" + tagNumber);
                    loadTag(tagNumber);
                }
            }
        }
    }
}

This likes 20 photos, then loads another hashtag, and repeats.

这喜欢 20 张照片,然后加载另一个主题标签,然后重复。

Any help with this would be great, thanks.

对此的任何帮助都会很棒,谢谢。

采纳答案by ItachiUchiha

The freeze is because you are trying to do everything on the same thread, when you are using Thread.sleep(delays), you are actually putting your UI thread to sleep and hence the lag !

冻结是因为您试图在同一线程上执行所有操作,当您使用 时Thread.sleep(delays),您实际上是在让 UI 线程进入睡眠状态,从而导致延迟!

Try using different thread for your work, you can either use a worker thread, if whatever you are doing will not affect the UI

尝试为您的工作使用不同的线程,您可以使用工作线程,如果您正在做的任何事情都不会影响 UI

or you can use Platform.runLater(), if you want the changes to shown on the UI !

或者您可以使用Platform.runLater(),如果您希望更改显示在 UI 上!

For more details :

更多细节 :

  1. concurrency in javafx !

  2. Platform.runLater and Task in JavaFX

  1. javafx中的并发!

  2. JavaFX 中的 Platform.runLater 和 Task