JavaFX在for循环中暂停而不使用Thread.Sleep() [英] JavaFX pausing during for loop WITHOUT using Thread.Sleep()

查看:661
本文介绍了JavaFX在for循环中暂停而不使用Thread.Sleep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FXMLController和场景构建器在JavaFX中编写这个代码。
我目前正在创建一个Instagram的liker,主要是为了练习,因为我是一个计算机科学专业,而我只是喜欢编码。



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

我能够使用这个延迟的唯一方法是通过线程。睡眠方法,但因为我在FXMLController编码,它冻结了整个用户界面,并以某种方式停止其他文本更新。这显然是不理想的。



我到处寻找替代品,但找不到任何在我的程序中有效的东西。



我给出整个方法:

  private void start(){ 
sleepWhen();
tagNumber ++;
int delays = Integer.valueOf(delay.getText());
延误=延误* 1000;
if(loaded){
runTime.clear();
runTime.appendText(喜欢...);
for(int i = 0; i <20; i ++){
webEngine.executeScript(document.getElementsByClassName('btn btn-default btn -xs likeButton')[+ i +] 。点击());
尝试{
double random = Math.random()* 1000;
random =随机+延迟;
delayed =(int)random;
System.out.println(延迟);
喜欢++;
likesNumber.clear();
likesNumber.appendText(String.valueOf(likes));
System.out.println(String.valueOf(likes));
Thread.sleep(延迟);
delays = Integer.valueOf(delay.getText());
延误=延误* 1000;
$ b $ catch(InterruptedException ex){
//什么也不做
}

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




$ b code $


这样喜欢20张照片,然后加载另一个hashtag,并重复。



任何帮助,这将是伟大的,谢谢。冻结是因为你正在尝试在同一个线程上执行所有操作,当你使用 Thread.sleep(delayed)时, code>,你实际上把你的UI线程睡觉,因此滞后!

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



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



更多详情:


  1. Platform.runLater和JavaFX中的任务


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.

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.

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.

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.

Any help with this would be great, thanks.

解决方案

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 !

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

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

For more details :

  1. concurrency in javafx !

  2. Platform.runLater and Task in JavaFX

这篇关于JavaFX在for循环中暂停而不使用Thread.Sleep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆