libGDX 中的 java 线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12671089/
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
java Threads in libGDX
提问by Mintybacon
Hey guys I am working on making a game with libGDX and I really want to thread the game so I am running a paint loop and a logic loop on separate threads much like you would making a simple java swing game with the paintcomponent loop and the runnable run loop
嘿伙计们,我正在使用 libGDX 制作游戏,我真的很想将游戏线程化,所以我在单独的线程上运行了一个绘制循环和一个逻辑循环,就像您使用 Paintcomponent 循环和 runnable 制作一个简单的 java 摇摆游戏一样运行循环
I am experienced with threading in c but not so much in java the only way I was able to make a thread was by making a class that extends threads and then creating the run loop in there
我对 c 中的线程有经验,但在 java 中没有那么多,我能够创建线程的唯一方法是创建一个扩展线程的类,然后在那里创建运行循环
but the point of making the run loop was to allow each screen to compute logic freely so I would end up needing some sort of abstractscreen class that has the custom thread class implemented
但制作运行循环的目的是让每个屏幕自由计算逻辑,所以我最终需要某种实现自定义线程类的抽象屏幕类
Im asking if there is a simpler or more standard way of implementing threads for this situation
我问是否有更简单或更标准的方法来实现这种情况的线程
回答by P.T.
The libGDX library already runs a separate render thread for the OpenGL context updates. See http://code.google.com/p/libgdx/wiki/TheArchitecture#The_Graphics_Module
libGDX 库已经为 OpenGL 上下文更新运行了一个单独的渲染线程。请参阅http://code.google.com/p/libgdx/wiki/TheArchitecture#The_Graphics_Module
We already learned that the UI thread is not executed continuously but only scheduled to run by the operating system if an event needs to be dispatched (roughly :p). That's why we instantiate a second thread we usually refer to as the rendering thread. This thread is created by the Graphics module that itself gets instantiated by the Application at startup.
我们已经了解到 UI 线程不是连续执行的,而是仅在需要调度事件时才由操作系统调度运行(大约 :p)。这就是我们实例化第二个线程的原因,我们通常将其称为渲染线程。该线程由 Graphics 模块创建,该模块本身在启动时由应用程序实例化。
The ApplicationListener.render()
method on your main game object will be invoked by this render thread once for every screen refresh (so it should be about 60hz), so just put the body of your render loop in your implementation of this method.
ApplicationListener.render()
每次屏幕刷新时,此渲染线程都会调用主游戏对象上的方法一次(因此它应该约为 60hz),因此只需将渲染循环的主体放入此方法的实现中。
You can create an additional background thread (e.g., for game logic) in the create
method of your ApplicationListener
(be sure to clean it up in the dispose
method). Other than the render
thread, I don't think any of the pre-existing threads would be the right place for game logic.
您可以在创建一个额外的后台线程(例如,对于游戏逻辑)create
的方法,你的ApplicationListener
(一定要清理的dispose
方法)。除了render
线程,我认为任何预先存在的线程都不适合游戏逻辑。
For communication between the threads, you can use any of the existing Java synchronization approaches. I've used Java's ArrayBlockingQueue<>
to send requests to a background thread. And I've used Gdx.app.postRunnable()
to get my background threads to push data to the render thread (such Runnables are run on the next frame, before render()
is invoked).
对于线程之间的通信,您可以使用任何现有的 Java 同步方法。我已经使用 JavaArrayBlockingQueue<>
向后台线程发送请求。而且我曾经Gdx.app.postRunnable()
让我的后台线程将数据推送到渲染线程(这样的 Runnable 在render()
调用之前的下一帧上运行)。
回答by Enno Shioji
Not sure what you mean, but it is standard that the drawing on the screen (reflect changes to the display) is done by a dedicated, single thread (e.g. see Swing framework). This is because it is quite difficult to let multiple threads draw on the screen without messing things up -- only few implementations do this. Hence, if you are planning to implement the GUI framework yourself, single threaded drawing approach would be recommendable.
不确定您的意思,但屏幕上的绘图(反映对显示的更改)是由专用的单线程完成的标准做法(例如,请参阅 Swing 框架)。这是因为让多个线程在屏幕上绘制而不搞乱是非常困难的——只有少数实现这样做。因此,如果您计划自己实现 GUI 框架,建议使用单线程绘图方法。
As for your business logic, it depends on many things. A very simple approach could indeed to spawn a new thread for each activity. These threads can then post the result to the said GUI thread. The problem with this approach is that your efficiency can drop significantly if many activities are concurrently executed at the same time, because in Java, every thread maps to a native OS thread. You can use thread pools to avoid that. Java standard library provides thread pools as ThreadPoolExecutor
or if you want a bit higher abstraction, ExecutorService
.
至于你的业务逻辑,它取决于很多事情。一个非常简单的方法确实可以为每个活动产生一个新线程。然后这些线程可以将结果发布到所述 GUI 线程。这种方法的问题在于,如果同时并发执行许多活动,您的效率会显着下降,因为在 Java 中,每个线程都映射到本地操作系统线程。您可以使用线程池来避免这种情况。Java 标准库提供线程池,ThreadPoolExecutor
或者如果您想要更高的抽象,ExecutorService
.