GUI更新之前线程休眠(Java 6) [英] Thread Sleeping Before GUI Updating (Java 6)

查看:110
本文介绍了GUI更新之前线程休眠(Java 6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void moveTo(Coordinate destination) {

    changeState(State.NAVIGATION);
    controlPnl.addRemote(Remote.createRemote(remoteType.NAVIGATION));

    dmc.moveTo(destination);

    changeState(State.IMMEDIATE);
    controlPnl.addRemote(Remote.createRemote(remoteType.IMMEDIATE));
}

在此代码中,addRemote方法使用新按钮更新controlPnl GUI. dmc.moveTo方法中最多包含两个Thread.sleep调用,并且我认为在controlPnl GUI更新之前已调用了它们.我已注释掉dmc.moveTo之后的两个方法调用,这两个方法将GUI更改为调用之前的状态,并且controlPnl直到moveTo完成执行后才完成更新.我需要的是GUI在moveTo方法开始执行并使线程进入睡眠状态之前完成更新.有什么方法可以在Java 6中完成?

In this code, the addRemote method updates the controlPnl GUI with new buttons. The dmc.moveTo method has up to two Thread.sleep calls in it, and I think that they are being called before the controlPnl GUI is being updated. I've commented out the two method calls after dmc.moveTo which change the GUI back to what it was before the call, and the controlPnl doesn't finish updating until moveTo finishes executing. What I need is for the GUI to finish updating before the moveTo method starts executing and puts the Thread to sleep. Is there any way that I could accomplish this in Java 6?

在必要时,moveTo方法将LEGO Mindstorm机器人移动到用户定义的路径上的指定点.正在更新的GUI提供了Swing组件(JButton和JRadioButtons),供用户在导航时控制机器人. addRemote方法更改了一组供用户使用的Swing组件,并且moveTo方法将命令发送给机器人以实际执行运动(通过告诉其电机运动,睡眠正确的时间,然后告知其电机进行运动).停止移动).我使用的是状态机模式,该方法是处理来自UI的事件的控制器的一部分.

In case it matters, the moveTo method moves a LEGO Mindstorm robot to a specified point on a path defined by the user. The GUI that is being updated provides Swing components (JButtons and JRadioButtons) for the user to control the robot with while it's navigating. The addRemote method changes the set of Swing components for the user to use, and the moveTo method sends commands to the robot to actually execute the movement (by telling its motors to move, sleeping for the correct amount of time, then telling its motors to stop moving). I'm using a state machine pattern, and this method is part of the controller which handles events from the UIs.

推荐答案

您只有一个GUI线程.不要用它来调用其他东西;如果这样做,则必须先完成这些步骤,然后GUI中才能进行其他任何操作.

You have a single GUI thread. Don't use it to call other things; if you do, those things have to complete before anything else is going to happen in your GUI.

至少您要启动一个新线程来执行您的dmc.moveTo(destination).这可能不是您执行此操作的唯一位置,并且可能希望使用

At the very least you would want to start a new thread to perform your dmc.moveTo(destination). More than likely this isn't the only place you're doing this, and probably want an Executor set up to perform these tasks.

在不了解您的代码的情况下(尤其是因为您使用的是静态方法),我无法评论您如何设置Executor,但是使用Thread的最简单示例是:

Without knowing more about your code (especially since you're using a static method) I can't comment on how you would want to set up the Executor but the simplest example of using a Thread would be:

 public static void moveTo(final Coordinate destination) {

    changeState(State.NAVIGATION);
    controlPnl.addRemote(Remote.createRemote(remoteType.NAVIGATION));

    new Thread(new Runnable() {
                   public void run() {
                       dmc.moveTo(destination);
                       changeState(State.IMMEDIATE);
                       controlPnl.addRemote(Remote.createRemote(remoteType.IMMEDIATE));
                   }
               }).start();
}

这将创建一个新的Thread,它执行您的(匿名)Runnable,该Runnable执行您的moveTo().注意,这要比准备运行任务的Executor效率低得多.它每次必须创建一个新的Thread.但是,如果这不是您需要的性能问题,那么就很好.还要注意,由于我直接在匿名内部类中引用destination,因此在传递给您的方法时必须将其声明为final.

This creates a new Thread that executes your (anonymous) Runnable which performs your moveTo(). Note this is far less efficient than having an Executor that is ready to run your task; it has to create a new Thread every time. However, if that's not an issue in terms of the performance you need then it's perfectly fine. Also note that because I'm referencing destination directly inside the anonymous inner class, it has to be declared final when passed into your method.

这篇关于GUI更新之前线程休眠(Java 6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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