带Swing的多线程 [英] Multi-threading with Swing

查看:133
本文介绍了带Swing的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swing编写多线程程序。实质上,程序的工作方式是运行时将有一个机器人(在屏幕快照中以圆圈表示),该机器人在一个字段中四处寻找。该机器人应由自己的线程控制。该程序具有按钮启动机器人。这将在场上创建另一个机器人(最多说10个)。现在,我已经有了该程序的基础知识,但是所有程序都在一个线程下运行。我可以启动任意数量的机器人,但是它们都在单个线程下运行。但是我希望每当我单击启动机器人创建一个独立的线程并控制该机器人。该程序的外观如下:

I am trying to write a multi-thread program with Swing. Essentially how the program works is that when it runs it will have a robot(represented by a circle in screenshot) that is wondering around in a field. This robot should be controlled by a thread of it's own. The program has a button "Launch Robot" that will create another robot on the field(upto a max of say 10). Right now I have the basics of the program, but it all runs under one thread. I can launch as many robots as I want but they all run under a single thread. But I want that whenever I click "launch Robot" an independent thread be created and control that robot. This is how the program looks right now:

该程序的UML图如下:

The UML diagram for the program is as following:

由于它有点长,所以我不会发布整个程序。但是,启动和更新机器人的方法(当前仅在现场控制一个机器人)如下:

Since its a bit long I won't post the whole program. But the method that starts and updates the robots(currently controlling only one robot on the field) is as follows:

    public void gameStart(){
    Thread gameThread = new Thread(){
        public void run(){
            while(true){
                //execute one time step for the game
                gameUpdate();
                
                //refresh screen
                repaint();
                
                //give other threads time
                try{
                    Thread.sleep(1000/UPDATE_RATE);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    };
    
    gameThread.start();
}

我的问题是如何在这种情况下实现多线程?我知道 SwingWorker 的基础知识,但是由于我没有做任何多线程处理,因此我不知道如何使多个线程工作并由一个线程进行更新(更新由线程控制的机器人的位置。)

My question is how can I achieve multi-threading for such a scenario? I know the basics of SwingWorker, but since I haven't done any multi-threading, I have no idea on how to make several threads work and be updated by one thread(update position of robots that are controlled by threads).

编辑:为了说明我的意思,这是我正在从事的项目。这与在这种情况下多线程是否有意义无关。

Just to make my point, this is a project that I am working on. It's not about if multi-threading makes sense in this scenario or not.

推荐答案

创建 RobotModel 包含 Collection< Robot> 并定义它们的交互。迭代doInBackground()实现中的模型 rel = nofollow noreferrer> SwingWorker 。在发生有意义的事件时调用 publish(),并且 process()更新到 RobotWorld 视图通过查询模型。如此处所讨论的那样,模型中不应包含绘图,视图中不应包含交互逻辑。 $ a可以满足一个中等复杂模型的需求,但是您可以如此处所示同步多个工人。

Create a RobotModel that contains a Collection<Robot> and defines their interaction. Iterate the model in the doInBackground() implementation of a SwingWorker. Invoke publish() as meaningful events arise, and process() updates to the RobotWorld view by querying the model. As discussed here, there should be no drawing in the model and no interaction logic in the view. A single worker should suffice for a moderately complex model, but you can synchronize multiple workers as shown here.

这篇关于带Swing的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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