如何避免for循环中的Thread.sleep()中断UI线程? [英] How to avoid Thread.sleep() in a for loop from interrupting the UI Thread?

查看:371
本文介绍了如何避免for循环中的Thread.sleep()中断UI线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下伪代码来阐明我的问题和解决方案.我的原始帖子和详细结果在Stack Overflow上,网址为:等待( )& Sleep()不能按预期工作.

I have the following pseudo code to clarify my problem and a solution. My original posting and detailed results are on Stack Overflow at: Wait() & Sleep() Not Working As Thought.

public class PixelArtSlideShow {  // called with click of Menu item.

   create List<File>  of each selected pixelArtFile
   for (File pixelArtFile : List<File>) {
      call displayFiles(pixelArtFile);
      TimeUnits.SECONDS.sleep(5); }
   }

    public static void displayFiles(File pixelArtFile) {

       for (loop array rows)
          for (loop array columns)
             read-in sRGB for each pixel - Circle Object
             window.setTitle(....)
}

// when above code is used to Open a pixelArtFile, it will appear instantly in  a 32 x 64 array

问题:在另一篇文章中有详细介绍.每个pixelArtFile将正确显示setTitle()并暂停约5秒钟,但是经过5秒钟后,除了最后一个文件外,Circle的颜色不会更改为指定的颜色.就像TimeUnits.SECONDS.sleep(5);中的所有代码一样.除window.setTitle(...)以外被跳过?

PROBLEM: As detailed extensively on the other post. Each pixelArtFile will display the setTitle() correctly and pause for about 5 secs but the Circle’s will not change to the assigned color except for the last file, after the 5 secs have passed. It's like all the code in the TimeUnits.SECONDS.sleep(5); are skipped EXCEPT the window.setTitle(...)?

我的理解是TimeUnits.SECONDS.sleep(5);无法控制UI线程的中断,我想必须以某种方式将其隔离以允许displayFiles(File pixelArtFile)完全执行.

My understanding is the TimeUnits.SECONDS.sleep(5); interrupts the UI Thread uncontrollable and I guess must somehow be isolated to allow the displayFiles(File pixelArtFile) to fully execute.

能否请您向我展示使用伪代码解决更完整解决方案的最直接方法?

Could you please show me the most straight forward way to solve this problem using the pseudo code for a more completed solution?

我尝试过RunnablesPlatform.runLater()FutureTask<Void>等,但是对于它们的工作方式和正确编码我感到非常困惑.

I have tried Runnables, Platform.runLater(), FutureTask<Void>, etc. and I'm pretty confused as to how they are meant to work and exactly coded.

我还在Web上发布了两个UI窗口:虚拟艺术.我认为像素阵列"窗口中显示的pixelArtFile可能可以解决问题.

I also have the two UI windows posted on the web at: Virtual Art. I think the pixelArtFile shown in the Pixel Array window may clarify the problem.

谢谢

推荐答案

不要休眠UI线程.时间轴可能会做您想要的.

Don't sleep the UI thread. A Timeline will probably do what you want.

List<File> files;
int curFileIdx = 0;

// prereq, files have been appropriately populated.
public void runAnimation() {
    Timeline timeline = new Timeline(
            new KeyFrame(Duration.seconds(5), event -> {
                if (!files.isEmpty()) {
                    displayFile(curFileIdx);
                    curFileIdx = (curFileIdx + 1) % files.size();
                }
            })
    );
    timeline.setCycleCount(Timeline.INDEFINITE);
    timeline.play();
}

// prereq, files have been appropriately populated.
public void displayFile(int idx) {
    File fileToDisplay = files.get(idx);
    // do your display logic.
}

注意,除了上述内容外,您可能还想运行一个单独的任务,用于将文件数据读入内存,并且只有一个List<ModelData>,其中ModelData是您从文件中读取数据的某种类.这样一来,您就不会在动画循环中连续运行IO.对于每帧五秒钟的动画,这可能没什么大不了的.但是,对于更频繁的动画,这种优化非常重要.

Note, in addition to the above, you probably want to run a separate task to read the file data into memory, and just have a List<ModelData> where ModelData is some class for data you have read from a file. That way you wouldn't be continuously running IO in your animation loop. For a five second per frame animation, it probably doesn't matter much. But, for a more frequent animation, such optimizations are very important.

这篇关于如何避免for循环中的Thread.sleep()中断UI线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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