LibGDX线程
2017-08-23 本文已影响18人
天神Deity
来自Threading的译文
ApplicationListener 所有的方法都是在同一个线程上运行的。该线程是供OpenGL调用的渲染线程.对大多数游戏来说,在ApplicationListener.render()方法中(渲染线程中调用)实现逻辑更新和渲染就足够了.
只有在渲染线程上才能执行任何直接涉及OpenGL的图形操作。 在其他线程上执行此操作会导致未定义的行为。 这是因为OpenGL上下文仅在渲染线程上处于活动状态。 在其他线程上执行图像操作在许多Android设备上会出现问题.
要从另一个线程传递数据到渲染线程,我们建议使用Application.postRunnable()。 这将在调用ApplicationListener.render()之前,在下一帧的渲染线程中运行Runnable中的代码。
new Thread(new Runnable() {
@Override
public void run() {
// do something important here, asynchronously to the rendering thread
final Result result = createResult();
// post a Runnable to the rendering thread that processes the result
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
// process the result, e.g. add it to an Array<Result> field of the ApplicationListener.
results.add(result);
}
});
}
}).start();
哪些libgdx类是线程安全的?
libgdx中没有类是线程安全的,除非在类文档中明确标记为线程安全。
您不应该对任何与图形或音频相关的内容执行多线程操作,例如 使用来自多个线程的scene2D组件。